gen_color.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <sstream>
  12. #include "api/pybind11_stl_include.h"
  13. #include "api/python_export_utils.h"
  14. #include "gen_color.h"
  15. #include "api/color.h"
  16. namespace MCell {
  17. namespace API {
  18. void GenColor::check_semantics() const {
  19. }
  20. void GenColor::set_initialized() {
  21. initialized = true;
  22. }
  23. void GenColor::set_all_attributes_as_default_or_unset() {
  24. class_name = "Color";
  25. red = FLT_UNSET;
  26. green = FLT_UNSET;
  27. blue = FLT_UNSET;
  28. alpha = 1;
  29. rgba = 0;
  30. }
  31. std::shared_ptr<Color> GenColor::copy_color() const {
  32. std::shared_ptr<Color> res = std::make_shared<Color>(DefaultCtorArgType());
  33. res->class_name = class_name;
  34. res->red = red;
  35. res->green = green;
  36. res->blue = blue;
  37. res->alpha = alpha;
  38. res->rgba = rgba;
  39. return res;
  40. }
  41. std::shared_ptr<Color> GenColor::deepcopy_color(py::dict) const {
  42. std::shared_ptr<Color> res = std::make_shared<Color>(DefaultCtorArgType());
  43. res->class_name = class_name;
  44. res->red = red;
  45. res->green = green;
  46. res->blue = blue;
  47. res->alpha = alpha;
  48. res->rgba = rgba;
  49. return res;
  50. }
  51. bool GenColor::__eq__(const Color& other) const {
  52. return
  53. red == other.red &&
  54. green == other.green &&
  55. blue == other.blue &&
  56. alpha == other.alpha &&
  57. rgba == other.rgba;
  58. }
  59. bool GenColor::eq_nonarray_attributes(const Color& other, const bool ignore_name) const {
  60. return
  61. red == other.red &&
  62. green == other.green &&
  63. blue == other.blue &&
  64. alpha == other.alpha &&
  65. rgba == other.rgba;
  66. }
  67. std::string GenColor::to_str(const bool all_details, const std::string ind) const {
  68. std::stringstream ss;
  69. ss << get_object_name() << ": " <<
  70. "red=" << red << ", " <<
  71. "green=" << green << ", " <<
  72. "blue=" << blue << ", " <<
  73. "alpha=" << alpha << ", " <<
  74. "rgba=" << rgba;
  75. return ss.str();
  76. }
  77. py::class_<Color> define_pybinding_Color(py::module& m) {
  78. return py::class_<Color, std::shared_ptr<Color>>(m, "Color", "Represents color with alpha component.\nProvides two means to set value, either red, green, blue and alpha, \nor rgba. If both color individual components and rgba are set in initialization,\nthe individual components are used.\n \n")
  79. .def(
  80. py::init<
  81. const double,
  82. const double,
  83. const double,
  84. const double,
  85. const uint
  86. >(),
  87. py::arg("red") = FLT_UNSET,
  88. py::arg("green") = FLT_UNSET,
  89. py::arg("blue") = FLT_UNSET,
  90. py::arg("alpha") = 1,
  91. py::arg("rgba") = 0
  92. )
  93. .def("check_semantics", &Color::check_semantics)
  94. .def("__copy__", &Color::copy_color)
  95. .def("__deepcopy__", &Color::deepcopy_color, py::arg("memo"))
  96. .def("__str__", &Color::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  97. .def("__eq__", &Color::__eq__, py::arg("other"))
  98. .def("dump", &Color::dump)
  99. .def_property("red", &Color::get_red, &Color::set_red, "Red component in range 0-1.")
  100. .def_property("green", &Color::get_green, &Color::set_green, "Green component in range 0-1.")
  101. .def_property("blue", &Color::get_blue, &Color::set_blue, "Blue component in range 0-1.")
  102. .def_property("alpha", &Color::get_alpha, &Color::set_alpha, "Alpha component in range 0-1. 1 means nontransparent.")
  103. .def_property("rgba", &Color::get_rgba, &Color::set_rgba, "This attribute provides an alternative way of defining colors by supplying a \n32-bit unsigned integer representation of the color with an aplha channel. \nIn hexadecimal notation the first 2 digits are value for red, second 2 digits are \ngreen, third 2 digits are blue and the last two digits are alpha. \nThe range for each component is thus 0x0-0xFF (0-255). \nExample: 0x0000ffcc represents the same color as rgba(0, 0, 100%, 80%).\nAll values are valid.\n \n")
  104. ;
  105. }
  106. std::string GenColor::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  107. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  108. return ctx.get_exported_name(this);
  109. }
  110. std::string exported_name = "color_" + std::to_string(ctx.postinc_counter("color"));
  111. if (!export_even_if_already_exported()) {
  112. ctx.add_exported(this, exported_name);
  113. }
  114. bool str_export = export_as_string_without_newlines();
  115. std::string nl = "";
  116. std::string ind = " ";
  117. std::stringstream ss;
  118. if (!str_export) {
  119. nl = "\n";
  120. ind = " ";
  121. ss << exported_name << " = ";
  122. }
  123. ss << "m.Color(" << nl;
  124. if (red != FLT_UNSET) {
  125. ss << ind << "red = " << f_to_str(red) << "," << nl;
  126. }
  127. if (green != FLT_UNSET) {
  128. ss << ind << "green = " << f_to_str(green) << "," << nl;
  129. }
  130. if (blue != FLT_UNSET) {
  131. ss << ind << "blue = " << f_to_str(blue) << "," << nl;
  132. }
  133. if (alpha != 1) {
  134. ss << ind << "alpha = " << f_to_str(alpha) << "," << nl;
  135. }
  136. if (rgba != 0) {
  137. ss << ind << "rgba = " << rgba << "," << nl;
  138. }
  139. ss << ")" << nl << nl;
  140. if (!str_export) {
  141. out << ss.str();
  142. return exported_name;
  143. }
  144. else {
  145. return ss.str();
  146. }
  147. }
  148. } // namespace API
  149. } // namespace MCell