color_mode.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**************************************************************************/
  2. /* color_mode.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "color_mode.h"
  31. #include "core/math/color.h"
  32. #include "scene/gui/slider.h"
  33. #include "thirdparty/misc/ok_color.h"
  34. ColorMode::ColorMode(ColorPicker *p_color_picker) {
  35. color_picker = p_color_picker;
  36. }
  37. String ColorModeRGB::get_slider_label(int idx) const {
  38. ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
  39. return labels[idx];
  40. }
  41. float ColorModeRGB::get_slider_max(int idx) const {
  42. ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
  43. Color color = color_picker->get_pick_color();
  44. return next_power_of_2(MAX(255, color.components[idx] * 255.0)) - 1;
  45. }
  46. float ColorModeRGB::get_slider_value(int idx) const {
  47. ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider value.");
  48. return color_picker->get_pick_color().components[idx] * 255;
  49. }
  50. Color ColorModeRGB::get_color() const {
  51. Vector<float> values = color_picker->get_active_slider_values();
  52. Color color;
  53. for (int i = 0; i < 4; i++) {
  54. color.components[i] = values[i] / 255.0;
  55. }
  56. return color;
  57. }
  58. void ColorModeRGB::slider_draw(int p_which) {
  59. Vector<Vector2> pos;
  60. pos.resize(4);
  61. Vector<Color> col;
  62. col.resize(4);
  63. HSlider *slider = color_picker->get_slider(p_which);
  64. Size2 size = slider->get_size();
  65. Color left_color;
  66. Color right_color;
  67. Color color = color_picker->get_pick_color();
  68. const real_t margin = 16 * color_picker->theme_cache.base_scale;
  69. if (p_which == ColorPicker::SLIDER_COUNT) {
  70. slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
  71. left_color = color;
  72. left_color.a = 0;
  73. right_color = color;
  74. right_color.a = 1;
  75. } else {
  76. left_color = Color(
  77. p_which == 0 ? 0 : color.r,
  78. p_which == 1 ? 0 : color.g,
  79. p_which == 2 ? 0 : color.b);
  80. right_color = Color(
  81. p_which == 0 ? 1 : color.r,
  82. p_which == 1 ? 1 : color.g,
  83. p_which == 2 ? 1 : color.b);
  84. }
  85. col.set(0, left_color);
  86. col.set(1, right_color);
  87. col.set(2, right_color);
  88. col.set(3, left_color);
  89. pos.set(0, Vector2(0, 0));
  90. pos.set(1, Vector2(size.x, 0));
  91. pos.set(2, Vector2(size.x, margin));
  92. pos.set(3, Vector2(0, margin));
  93. slider->draw_polygon(pos, col);
  94. }
  95. void ColorModeHSV::_value_changed() {
  96. Vector<float> values = color_picker->get_active_slider_values();
  97. if (values[1] > 0 || values[0] != cached_hue) {
  98. cached_hue = values[0];
  99. }
  100. if (values[2] > 0 || values[1] != cached_saturation) {
  101. cached_saturation = values[1];
  102. }
  103. }
  104. String ColorModeHSV::get_slider_label(int idx) const {
  105. ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
  106. return labels[idx];
  107. }
  108. float ColorModeHSV::get_slider_max(int idx) const {
  109. ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
  110. return slider_max[idx];
  111. }
  112. float ColorModeHSV::get_slider_value(int idx) const {
  113. switch (idx) {
  114. case 0: {
  115. if (color_picker->get_pick_color().get_s() > 0) {
  116. return color_picker->get_pick_color().get_h() * 360.0;
  117. } else {
  118. return cached_hue;
  119. }
  120. }
  121. case 1: {
  122. if (color_picker->get_pick_color().get_v() > 0) {
  123. return color_picker->get_pick_color().get_s() * 100.0;
  124. } else {
  125. return cached_saturation;
  126. }
  127. }
  128. case 2:
  129. return color_picker->get_pick_color().get_v() * 100.0;
  130. case 3:
  131. return Math::round(color_picker->get_pick_color().components[3] * 255.0);
  132. default:
  133. ERR_FAIL_V_MSG(0, "Couldn't get slider value.");
  134. }
  135. }
  136. Color ColorModeHSV::get_color() const {
  137. Vector<float> values = color_picker->get_active_slider_values();
  138. Color color;
  139. color.set_hsv(values[0] / 360.0, values[1] / 100.0, values[2] / 100.0, values[3] / 255.0);
  140. return color;
  141. }
  142. void ColorModeHSV::slider_draw(int p_which) {
  143. Vector<Vector2> pos;
  144. pos.resize(4);
  145. Vector<Color> col;
  146. col.resize(4);
  147. HSlider *slider = color_picker->get_slider(p_which);
  148. Size2 size = slider->get_size();
  149. Color left_color;
  150. Color right_color;
  151. Color color = color_picker->get_pick_color();
  152. const real_t margin = 16 * color_picker->theme_cache.base_scale;
  153. if (p_which == ColorPicker::SLIDER_COUNT) {
  154. slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
  155. left_color = color;
  156. left_color.a = 0;
  157. right_color = color;
  158. right_color.a = 1;
  159. } else if (p_which == 0) {
  160. float v = color.get_v();
  161. left_color = Color(v, v, v);
  162. right_color = left_color;
  163. } else {
  164. Color s_col;
  165. Color v_col;
  166. s_col.set_hsv(color.get_h(), 0, color.get_v());
  167. left_color = (p_which == 1) ? s_col : Color(0, 0, 0);
  168. float s_col_hue = (Math::is_zero_approx(color.get_s())) ? cached_hue / 360.0 : color.get_h();
  169. s_col.set_hsv(s_col_hue, 1, color.get_v());
  170. v_col.set_hsv(color.get_h(), color.get_s(), 1);
  171. right_color = (p_which == 1) ? s_col : v_col;
  172. }
  173. col.set(0, left_color);
  174. col.set(1, right_color);
  175. col.set(2, right_color);
  176. col.set(3, left_color);
  177. pos.set(0, Vector2(0, 0));
  178. pos.set(1, Vector2(size.x, 0));
  179. pos.set(2, Vector2(size.x, margin));
  180. pos.set(3, Vector2(0, margin));
  181. slider->draw_polygon(pos, col);
  182. if (p_which == 0) { // H
  183. Ref<Texture2D> hue = color_picker->theme_cache.color_hue;
  184. slider->draw_texture_rect(hue, Rect2(Vector2(), Vector2(size.x, margin)), false, Color::from_hsv(0, 0, color.get_v(), color.get_s()));
  185. }
  186. }
  187. String ColorModeRAW::get_slider_label(int idx) const {
  188. ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
  189. return labels[idx];
  190. }
  191. float ColorModeRAW::get_slider_max(int idx) const {
  192. ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
  193. return slider_max[idx];
  194. }
  195. float ColorModeRAW::get_slider_value(int idx) const {
  196. ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider value.");
  197. return color_picker->get_pick_color().components[idx];
  198. }
  199. Color ColorModeRAW::get_color() const {
  200. Vector<float> values = color_picker->get_active_slider_values();
  201. Color color;
  202. for (int i = 0; i < 4; i++) {
  203. color.components[i] = values[i];
  204. }
  205. return color;
  206. }
  207. void ColorModeRAW::slider_draw(int p_which) {
  208. Vector<Vector2> pos;
  209. pos.resize(4);
  210. Vector<Color> col;
  211. col.resize(4);
  212. HSlider *slider = color_picker->get_slider(p_which);
  213. Size2 size = slider->get_size();
  214. Color left_color;
  215. Color right_color;
  216. Color color = color_picker->get_pick_color();
  217. const real_t margin = 16 * color_picker->theme_cache.base_scale;
  218. if (p_which == ColorPicker::SLIDER_COUNT) {
  219. slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
  220. left_color = color;
  221. left_color.a = 0;
  222. right_color = color;
  223. right_color.a = 1;
  224. col.set(0, left_color);
  225. col.set(1, right_color);
  226. col.set(2, right_color);
  227. col.set(3, left_color);
  228. pos.set(0, Vector2(0, 0));
  229. pos.set(1, Vector2(size.x, 0));
  230. pos.set(2, Vector2(size.x, margin));
  231. pos.set(3, Vector2(0, margin));
  232. slider->draw_polygon(pos, col);
  233. }
  234. }
  235. bool ColorModeRAW::apply_theme() const {
  236. for (int i = 0; i < 4; i++) {
  237. HSlider *slider = color_picker->get_slider(i);
  238. slider->remove_theme_icon_override("grabber");
  239. slider->remove_theme_icon_override("grabber_highlight");
  240. slider->remove_theme_style_override("slider");
  241. slider->remove_theme_constant_override("grabber_offset");
  242. }
  243. return true;
  244. }
  245. void ColorModeOKHSL::_value_changed() {
  246. Vector<float> values = color_picker->get_active_slider_values();
  247. if (values[1] > 0 || values[0] != cached_hue) {
  248. cached_hue = values[0];
  249. }
  250. if (values[2] > 0 || values[1] != cached_saturation) {
  251. cached_saturation = values[1];
  252. }
  253. }
  254. String ColorModeOKHSL::get_slider_label(int idx) const {
  255. ERR_FAIL_INDEX_V_MSG(idx, 3, String(), "Couldn't get slider label.");
  256. return labels[idx];
  257. }
  258. float ColorModeOKHSL::get_slider_max(int idx) const {
  259. ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
  260. return slider_max[idx];
  261. }
  262. float ColorModeOKHSL::get_slider_value(int idx) const {
  263. switch (idx) {
  264. case 0: {
  265. if (color_picker->get_pick_color().get_ok_hsl_s() > 0) {
  266. return color_picker->get_pick_color().get_ok_hsl_h() * 360.0;
  267. } else {
  268. return cached_hue;
  269. }
  270. }
  271. case 1: {
  272. if (color_picker->get_pick_color().get_ok_hsl_l() > 0) {
  273. return color_picker->get_pick_color().get_ok_hsl_s() * 100.0;
  274. } else {
  275. return cached_saturation;
  276. }
  277. }
  278. case 2:
  279. return color_picker->get_pick_color().get_ok_hsl_l() * 100.0;
  280. case 3:
  281. return Math::round(color_picker->get_pick_color().components[3] * 255.0);
  282. default:
  283. ERR_FAIL_V_MSG(0, "Couldn't get slider value.");
  284. }
  285. }
  286. Color ColorModeOKHSL::get_color() const {
  287. Vector<float> values = color_picker->get_active_slider_values();
  288. Color color;
  289. color.set_ok_hsl(values[0] / 360.0, values[1] / 100.0, values[2] / 100.0, values[3] / 255.0);
  290. return color;
  291. }
  292. void ColorModeOKHSL::slider_draw(int p_which) {
  293. HSlider *slider = color_picker->get_slider(p_which);
  294. Size2 size = slider->get_size();
  295. const real_t margin = 16 * color_picker->theme_cache.base_scale;
  296. Vector<Vector2> pos;
  297. Vector<Color> col;
  298. Color left_color;
  299. Color right_color;
  300. Color color = color_picker->get_pick_color();
  301. if (p_which == 2) { // L
  302. pos.resize(6);
  303. col.resize(6);
  304. left_color = Color(0, 0, 0);
  305. Color middle_color;
  306. float slider_hue = (Math::is_zero_approx(color.get_ok_hsl_s())) ? cached_hue / 360.0 : color.get_ok_hsl_h();
  307. float slider_sat = (Math::is_zero_approx(color.get_ok_hsl_l())) ? cached_saturation / 100.0 : color.get_ok_hsl_s();
  308. middle_color.set_ok_hsl(slider_hue, slider_sat, 0.5);
  309. right_color.set_ok_hsl(slider_hue, slider_sat, 1);
  310. col.set(0, left_color);
  311. col.set(1, middle_color);
  312. col.set(2, right_color);
  313. col.set(3, right_color);
  314. col.set(4, middle_color);
  315. col.set(5, left_color);
  316. pos.set(0, Vector2(0, 0));
  317. pos.set(1, Vector2(size.x * 0.5, 0));
  318. pos.set(2, Vector2(size.x, 0));
  319. pos.set(3, Vector2(size.x, margin));
  320. pos.set(4, Vector2(size.x * 0.5, margin));
  321. pos.set(5, Vector2(0, margin));
  322. } else {
  323. pos.resize(4);
  324. col.resize(4);
  325. if (p_which == ColorPicker::SLIDER_COUNT) {
  326. slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
  327. left_color = color;
  328. left_color.a = 0;
  329. right_color = color;
  330. right_color.a = 1;
  331. } else if (p_which == 0) {
  332. float l = color.get_ok_hsl_l();
  333. left_color = Color(l, l, l);
  334. right_color = left_color;
  335. } else {
  336. left_color.set_ok_hsl(color.get_ok_hsl_h(), 0, color.get_ok_hsl_l());
  337. float s_col_hue = (Math::is_zero_approx(color.get_ok_hsl_s())) ? cached_hue / 360.0 : color.get_ok_hsl_h();
  338. right_color.set_ok_hsl(s_col_hue, 1, color.get_ok_hsl_l());
  339. }
  340. col.set(0, left_color);
  341. col.set(1, right_color);
  342. col.set(2, right_color);
  343. col.set(3, left_color);
  344. pos.set(0, Vector2(0, 0));
  345. pos.set(1, Vector2(size.x, 0));
  346. pos.set(2, Vector2(size.x, margin));
  347. pos.set(3, Vector2(0, margin));
  348. }
  349. slider->draw_polygon(pos, col);
  350. if (p_which == 0) { // H
  351. Ref<Texture2D> hue = color_picker->theme_cache.color_okhsl_hue;
  352. slider->draw_texture_rect(hue, Rect2(Vector2(), Vector2(size.x, margin)), false, Color::from_hsv(0, 0, color.get_ok_hsl_l() * 2.0, color.get_ok_hsl_s()));
  353. return;
  354. }
  355. }