color.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2021 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #include "api/color.h"
  12. #include "geometry.h"
  13. namespace MCell {
  14. namespace API {
  15. void Color::postprocess_in_ctor() {
  16. if (is_set(red) && is_set(green) && is_set(blue) && is_set(alpha)) {
  17. check_component_range(red, NAME_RED);
  18. check_component_range(green, NAME_GREEN);
  19. check_component_range(blue, NAME_BLUE);
  20. check_component_range(alpha, NAME_ALPHA);
  21. components_to_rgba();
  22. }
  23. else {
  24. bool any_component_set = is_set(red) || is_set(green) || is_set(blue);
  25. if (any_component_set) {
  26. throw ValueError(S("Either all individual color components must be set or none in initialization of ") +
  27. NAME_CLASS_COLOR + ".");
  28. }
  29. rgba_to_components();
  30. }
  31. }
  32. void Color::check_component_range(const double value, const char* name) {
  33. if (value < 0 || value > 1) {
  34. throw ValueError(S("Value of color component ") + name + " must be within 0..1.");
  35. }
  36. }
  37. void Color::components_to_rgba() {
  38. const int MAX = 255;
  39. rgba =
  40. (((uint)(red * MAX) & 0xFF) << 24) |
  41. (((uint)(green * MAX) & 0xFF) << 16) |
  42. (((uint)(blue * MAX) & 0xFF) << 8) |
  43. (((uint)(alpha * MAX)) & 0xFF);
  44. }
  45. void Color::rgba_to_components() {
  46. Geometry::rgba_to_components(rgba, red, green, blue, alpha);
  47. }
  48. } // namespace API
  49. } // namespace MCell