colorpicker.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <iostream>
  17. #include <ClanLib/Display/keys.h>
  18. #include <ClanLib/GUI/component.h>
  19. #include "colorpicker.hpp"
  20. #include "math.hpp"
  21. class ColorPickerHue : public CL_Component
  22. {
  23. public:
  24. std::vector<CL_Slot> slots;
  25. typedef std::vector<CL_Color> Colors;
  26. Colors colors;
  27. bool pressed;
  28. CL_Signal_v1<CL_Color> on_color_change;
  29. ColorPickerHue(const CL_Rect& rect, CL_Component* parent)
  30. : CL_Component(rect, parent),
  31. pressed(false)
  32. {
  33. colors.push_back(CL_Color(255, 0, 0));
  34. colors.push_back(CL_Color(255, 0, 255));
  35. colors.push_back(CL_Color( 0, 0, 255));
  36. colors.push_back(CL_Color( 0, 255, 255));
  37. colors.push_back(CL_Color( 0, 255, 0));
  38. colors.push_back(CL_Color(255, 255, 0));
  39. colors.push_back(CL_Color(255, 0, 0));
  40. slots.push_back(sig_paint().connect(this, &ColorPickerHue::draw));
  41. slots.push_back(sig_mouse_down().connect(this, &ColorPickerHue::on_mouse_down));
  42. slots.push_back(sig_mouse_up().connect(this, &ColorPickerHue::on_mouse_up));
  43. slots.push_back(sig_mouse_move().connect(this, &ColorPickerHue::on_mouse_move));
  44. }
  45. void update_pointer(const CL_InputEvent& event)
  46. {
  47. CL_Color new_color;
  48. if (event.mouse_pos.y >= get_height() || event.mouse_pos.y < 0)
  49. {
  50. new_color = colors[0];
  51. }
  52. else
  53. {
  54. float factor = (float(event.mouse_pos.y) / get_height()) * (colors.size()-1);
  55. int prevcol = int(factor);
  56. int nextcol = prevcol+1;
  57. float val = factor - prevcol;
  58. float ival = 1.0f - val;
  59. if (val >= 0 && val < 1.0f)
  60. {
  61. new_color = CL_Color(int(val * colors[nextcol].get_red() + ival * colors[prevcol].get_red()),
  62. int(val * colors[nextcol].get_green() + ival * colors[prevcol].get_green()),
  63. int(val * colors[nextcol].get_blue() + ival * colors[prevcol].get_blue()),
  64. int(val * colors[nextcol].get_alpha() + ival * colors[prevcol].get_alpha()));
  65. }
  66. else
  67. {
  68. std::cout << "Out of range" << std::endl;
  69. new_color = colors[0];
  70. }
  71. }
  72. on_color_change(new_color);
  73. /*
  74. std::cout << new_color.get_red() << ", "
  75. << new_color.get_green() << ", "
  76. << new_color.get_blue() << ", "
  77. << new_color.get_alpha()
  78. << std::endl;*/
  79. }
  80. void on_mouse_up(const CL_InputEvent& event)
  81. {
  82. if (event.id == CL_MOUSE_LEFT)
  83. {
  84. pressed = false;
  85. release_mouse();
  86. update_pointer(event);
  87. }
  88. }
  89. void on_mouse_down(const CL_InputEvent& event)
  90. {
  91. if (event.id == CL_MOUSE_LEFT)
  92. {
  93. pressed = true;
  94. capture_mouse();
  95. update_pointer(event);
  96. }
  97. }
  98. void on_mouse_move(const CL_InputEvent& event)
  99. {
  100. if (pressed)
  101. {
  102. update_pointer(event);
  103. }
  104. }
  105. void draw()
  106. {
  107. CL_Display::push_modelview();
  108. CL_Display::add_translate(get_screen_x(), get_screen_y());
  109. int psize = get_height()/6;
  110. for(Colors::size_type i = 0; i < colors.size()-1; ++i)
  111. {
  112. CL_Display::fill_rect(CL_Rect(CL_Point(0, i*psize),
  113. CL_Size(get_width(), psize)),
  114. CL_Gradient(colors[i],
  115. colors[i],
  116. colors[i+1],
  117. colors[i+1]));
  118. }
  119. CL_Display::pop_modelview();
  120. }
  121. };
  122. class ColorPickerAlpha : public CL_Component
  123. {
  124. public:
  125. std::vector<CL_Slot> slots;
  126. bool pressed;
  127. CL_Signal_v1<float> on_color_change;
  128. float alpha;
  129. ColorPickerAlpha(const CL_Rect& rect, CL_Component* parent)
  130. : CL_Component(rect, parent),
  131. pressed(false),
  132. alpha(0.5f)
  133. {
  134. slots.push_back(sig_paint().connect(this, &ColorPickerAlpha::draw));
  135. slots.push_back(sig_mouse_down().connect(this, &ColorPickerAlpha::on_mouse_down));
  136. slots.push_back(sig_mouse_up().connect(this, &ColorPickerAlpha::on_mouse_up));
  137. slots.push_back(sig_mouse_move().connect(this, &ColorPickerAlpha::on_mouse_move));
  138. }
  139. void set_alpha(float alpha_)
  140. {
  141. alpha = alpha_;
  142. on_color_change(alpha);
  143. }
  144. void draw()
  145. {
  146. CL_Display::push_modelview();
  147. CL_Display::add_translate(get_screen_x(), get_screen_y());
  148. CL_Display::fill_rect(CL_Rect(CL_Point(0, 0),
  149. CL_Size(get_width(), get_height())),
  150. CL_Gradient(CL_Color(0, 0, 0),
  151. CL_Color(255, 255, 255),
  152. CL_Color(0, 0, 0),
  153. CL_Color(255, 255, 255)));
  154. CL_Display::pop_modelview();
  155. }
  156. void update_pointer(const CL_InputEvent& event)
  157. {
  158. alpha = 1.0f - (Math::mid(0.0f, float(event.mouse_pos.x) / get_width(), 1.0f));
  159. on_color_change(alpha);
  160. }
  161. void on_mouse_up(const CL_InputEvent& event)
  162. {
  163. if (event.id == CL_MOUSE_LEFT)
  164. {
  165. pressed = false;
  166. release_mouse();
  167. update_pointer(event);
  168. }
  169. }
  170. void on_mouse_down(const CL_InputEvent& event)
  171. {
  172. if (event.id == CL_MOUSE_LEFT)
  173. {
  174. pressed = true;
  175. capture_mouse();
  176. update_pointer(event);
  177. }
  178. }
  179. void on_mouse_move(const CL_InputEvent& event)
  180. {
  181. if (pressed)
  182. {
  183. update_pointer(event);
  184. }
  185. }
  186. };
  187. class ColorPickerBrightness : public CL_Component
  188. {
  189. public:
  190. std::vector<CL_Slot> slots;
  191. CL_Color color;
  192. bool pressed;
  193. CL_Signal_v1<CL_Color> on_color_change;
  194. float factor_x;
  195. float factor_y;
  196. ColorPickerBrightness(const CL_Rect& rect, CL_Component* parent)
  197. : CL_Component(rect, parent),
  198. pressed(false),
  199. factor_x(1.0f),
  200. factor_y(1.0f)
  201. {
  202. color = CL_Color(255, 0, 0);
  203. slots.push_back(sig_paint().connect(this, &ColorPickerBrightness::draw));
  204. slots.push_back(sig_mouse_down().connect(this, &ColorPickerBrightness::on_mouse_down));
  205. slots.push_back(sig_mouse_up().connect(this, &ColorPickerBrightness::on_mouse_up));
  206. slots.push_back(sig_mouse_move().connect(this, &ColorPickerBrightness::on_mouse_move));
  207. }
  208. void draw()
  209. {
  210. CL_Display::push_modelview();
  211. CL_Display::add_translate(get_screen_x(), get_screen_y());
  212. CL_Display::fill_rect(CL_Rect(CL_Point(0, 0),
  213. CL_Size(get_width(), get_height())),
  214. CL_Gradient(CL_Color(0, 0, 0),
  215. color,
  216. CL_Color(0, 0, 0),
  217. CL_Color(255, 255, 255)));
  218. CL_Display::draw_line(factor_x * get_width(),
  219. 0,
  220. factor_x * get_width(),
  221. get_height(),
  222. CL_Color(255, 255, 255));
  223. CL_Display::draw_line(0,
  224. factor_y * get_height(),
  225. get_width(),
  226. factor_y * get_height(),
  227. CL_Color(255, 255, 255));
  228. CL_Display::pop_modelview();
  229. }
  230. void set_color(CL_Color color_) {
  231. color = color_;
  232. update_color();
  233. }
  234. void update_color()
  235. {
  236. CL_Color new_color(Math::mid(0, int(factor_x * color.get_red() * (1.0f - factor_y) + factor_x * 255 * (factor_y)), 255),
  237. Math::mid(0, int(factor_x * color.get_green() * (1.0f - factor_y) + factor_x * 255 * (factor_y)), 255),
  238. Math::mid(0, int(factor_x * color.get_blue() * (1.0f - factor_y) + factor_x * 255 * (factor_y)), 255),
  239. color.get_alpha());
  240. on_color_change(new_color);
  241. /*
  242. std::cout << new_color.get_red() << ", "
  243. << new_color.get_green() << ", "
  244. << new_color.get_blue() << ", "
  245. << new_color.get_alpha()
  246. << std::endl;
  247. */
  248. }
  249. void update_pointer(const CL_InputEvent& event)
  250. {
  251. factor_x = Math::mid(0.0f, float(event.mouse_pos.x)/get_width(), 1.0f);
  252. factor_y = Math::mid(0.0f, float(event.mouse_pos.y)/get_height(), 1.0f);
  253. update_color();
  254. }
  255. void on_mouse_up(const CL_InputEvent& event)
  256. {
  257. if (event.id == CL_MOUSE_LEFT)
  258. {
  259. pressed = false;
  260. release_mouse();
  261. update_pointer(event);
  262. }
  263. }
  264. void on_mouse_down(const CL_InputEvent& event)
  265. {
  266. if (event.id == CL_MOUSE_LEFT)
  267. {
  268. pressed = true;
  269. capture_mouse();
  270. update_pointer(event);
  271. }
  272. }
  273. void on_mouse_move(const CL_InputEvent& event)
  274. {
  275. if (pressed)
  276. {
  277. update_pointer(event);
  278. }
  279. }
  280. };
  281. ColorPicker::ColorPicker(const CL_Rect& rect, CL_Component* parent)
  282. : CL_Component(rect, parent)
  283. {
  284. float pwidth = rect.get_width()/11.0;
  285. float pheight = rect.get_height()/11.0;
  286. brightness = new ColorPickerBrightness(CL_Rect(CL_Point(0, 0),
  287. CL_Size(int(pwidth*10), int(pheight*10))),
  288. this);
  289. hue = new ColorPickerHue(CL_Rect(CL_Point(int(pwidth*10), 0),
  290. CL_Size(int(pwidth*1), int(pheight*10))),
  291. this);
  292. alpha = new ColorPickerAlpha(CL_Rect(CL_Point(0, int(pheight*10)),
  293. CL_Size(int(pwidth*10), int(pheight*1))),
  294. this);
  295. slots.push_back(hue->on_color_change.connect(brightness, &ColorPickerBrightness::set_color));
  296. slots.push_back(brightness->on_color_change.connect(this, &ColorPicker::update_brightness_color));
  297. slots.push_back(alpha->on_color_change.connect(this, &ColorPicker::update_alpha_color));
  298. slots.push_back(sig_paint().connect(this, &ColorPicker::draw));
  299. brightness->set_color(CL_Color(255, 0, 0));
  300. alpha->set_alpha(0.5f);
  301. }
  302. void
  303. ColorPicker::update_alpha_color(float alpha)
  304. {
  305. color.set_alpha(int(255 * alpha));
  306. on_color_change(color);
  307. }
  308. void
  309. ColorPicker::update_brightness_color(CL_Color color_)
  310. {
  311. color.set_red(color_.get_red());
  312. color.set_green(color_.get_green());
  313. color.set_blue(color_.get_blue());
  314. on_color_change(color);
  315. }
  316. void
  317. ColorPicker::draw()
  318. {
  319. CL_Display::push_modelview();
  320. CL_Display::add_translate(get_screen_x(), get_screen_y());
  321. float pwidth = get_width()/11.0;
  322. float pheight = get_height()/11.0;
  323. CL_Display::fill_rect(CL_Rect(CL_Point(int(pwidth*10), int(pheight*10)),
  324. CL_Size(int(pwidth), int(pheight))),
  325. color);
  326. CL_Display::pop_modelview();
  327. }
  328. CL_Signal_v1<CL_Color>&
  329. ColorPicker::sig_color_change()
  330. {
  331. return on_color_change;
  332. }
  333. CL_Color
  334. ColorPicker::get_color()
  335. {
  336. return color;
  337. }
  338. void
  339. ColorPicker::set_color(const CL_Color& color_)
  340. {
  341. color = color_;
  342. }
  343. /* EOF */