input_color3.vala 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. extern GLib.Object gtk_color_picker_new();
  6. extern void gtk_color_picker_pick(GLib.Object picker, GLib.AsyncReadyCallback callback);
  7. extern Gdk.RGBA? gtk_color_picker_pick_finish(GLib.Object picker, GLib.AsyncResult result, ref GLib.Error error);
  8. namespace Crown
  9. {
  10. public class ColorButton : Gtk.MenuButton
  11. {
  12. public Gdk.RGBA color = { 1.0, 1.0, 1.0, 1.0 };
  13. public const int PADDING = 4;
  14. public ColorButton()
  15. {
  16. Object();
  17. }
  18. public void set_color(Gdk.RGBA c)
  19. {
  20. this.color = c;
  21. this.queue_draw();
  22. }
  23. public override bool draw(Cairo.Context cr)
  24. {
  25. base.draw(cr);
  26. Gtk.Allocation alloc;
  27. this.get_allocation(out alloc);
  28. cr.set_source_rgba(this.color.red, this.color.green, this.color.blue, this.color.alpha);
  29. cr.rectangle(PADDING, PADDING, alloc.width - 2 * PADDING, alloc.height - 2 * PADDING);
  30. cr.fill();
  31. return false;
  32. }
  33. }
  34. static void hsv_to_rgb(ref double r, ref double g, ref double b, double h, double s, double v)
  35. {
  36. assert(h >= 0.0 && s >= 0.0 && v >= 0.0);
  37. if (s == 0.0) {
  38. r = g = b = v;
  39. return;
  40. }
  41. if (h >= 1.0)
  42. h = 0.0;
  43. double hf = h * 6.0;
  44. int i = (int)MathUtils.floor(hf);
  45. double f = MathUtils.fract(hf);
  46. double p = v * (1.0 - s);
  47. double q = v * (1.0 - s * f);
  48. double t = v * (1.0 - s * (1.0 - f));
  49. switch (i) {
  50. case 0: r = v; g = t; b = p; break;
  51. case 1: r = q; g = v; b = p; break;
  52. case 2: r = p; g = v; b = t; break;
  53. case 3: r = p; g = q; b = v; break;
  54. case 4: r = t; g = p; b = v; break;
  55. case 5: r = v; g = p; b = q; break;
  56. default: assert(false); break;
  57. }
  58. }
  59. static void rgb_to_hsv(ref double h, ref double s, ref double v, double r, double g, double b)
  60. {
  61. assert(r >= 0.0 && g >= 0.0 && b >= 0.0);
  62. double max = double.max(double.max(r, g), b);
  63. double min = double.min(double.min(r, g), b);
  64. double delta = max - min;
  65. v = max;
  66. if (max == 0.0) {
  67. s = 0.0;
  68. h = 0.0;
  69. return;
  70. }
  71. s = (delta <= 0.0) ? 0.0 : (delta / max);
  72. if (delta <= 1e-6) {
  73. h = 0.0;
  74. } else {
  75. double hh;
  76. if (max == r) {
  77. hh = (g - b) / delta;
  78. if (hh < 0.0) hh += 6.0;
  79. } else if (max == g) {
  80. hh = (b - r) / delta + 2.0;
  81. } else { // max == b
  82. hh = (r - g) / delta + 4.0;
  83. }
  84. hh = (hh / 6.0).clamp(0.0, 1.0);
  85. if (hh >= 1.0)
  86. hh = 0.0;
  87. h = hh;
  88. }
  89. }
  90. const int HS_PALETTE_SEGMENTS = 24;
  91. const Vector3 primary_colors[] =
  92. {
  93. { 1.0, 0.0, 0.0 },
  94. { 1.0, 1.0, 0.0 },
  95. { 0.0, 1.0, 0.0 },
  96. { 0.0, 1.0, 1.0 },
  97. { 0.0, 0.0, 1.0 },
  98. { 1.0, 0.0, 1.0 }
  99. };
  100. static double norm_angle(double a)
  101. {
  102. double x = Math.fmod(a, PI_TWO);
  103. if (x < 0)
  104. x += PI_TWO;
  105. return x;
  106. }
  107. static double angle_midpoint(double a, double b)
  108. {
  109. double delta = b - a;
  110. if (delta <= -Math.PI)
  111. delta += PI_TWO;
  112. else if (delta > Math.PI)
  113. delta -= PI_TWO;
  114. return a + delta * 0.5;
  115. }
  116. static void color_from_anchors(out Vector3 rgb, double theta)
  117. {
  118. double theta_n = norm_angle(theta);
  119. const double seg = Math.PI / 3.0;
  120. int idx = ((int)Math.floor(theta_n / seg)).clamp(0, 5);
  121. double anchor_a = (double)idx * seg;
  122. int next = (idx + 1) % 6;
  123. double t = ((theta_n - anchor_a) / seg).clamp(0.0, 1.0);
  124. rgb.x = MathUtils.lerp(primary_colors[idx].x, primary_colors[next].x, t);
  125. rgb.y = MathUtils.lerp(primary_colors[idx].y, primary_colors[next].y, t);
  126. rgb.z = MathUtils.lerp(primary_colors[idx].z, primary_colors[next].z, t);
  127. }
  128. static Cairo.MeshPattern create_circle_mesh(double cx, double cy, double r, double hsv_value)
  129. {
  130. Cairo.MeshPattern mesh = new Cairo.MeshPattern();
  131. double angles[HS_PALETTE_SEGMENTS];
  132. double ox[HS_PALETTE_SEGMENTS];
  133. double oy[HS_PALETTE_SEGMENTS];
  134. Vector3 cols[HS_PALETTE_SEGMENTS];
  135. for (int i = 0; i < HS_PALETTE_SEGMENTS; ++i) {
  136. double theta = PI_TWO * (double)i / (double)HS_PALETTE_SEGMENTS;
  137. angles[i] = theta;
  138. ox[i] = cx + r * Math.cos(theta);
  139. oy[i] = cy - r * Math.sin(theta);
  140. Vector3 base_col;
  141. color_from_anchors(out base_col, theta);
  142. cols[i].x = base_col.x * hsv_value;
  143. cols[i].y = base_col.y * hsv_value;
  144. cols[i].z = base_col.z * hsv_value;
  145. }
  146. for (int i = 0; i < HS_PALETTE_SEGMENTS; ++i) {
  147. int inext = (i + 1) % HS_PALETTE_SEGMENTS;
  148. double angle_mid = angle_midpoint(angles[i], angles[inext]);
  149. double mx = cx + r * Math.cos(angle_mid);
  150. double my = cy - r * Math.sin(angle_mid);
  151. mesh.begin_patch();
  152. mesh.move_to(cx, cy);
  153. mesh.line_to(ox[i], oy[i]);
  154. mesh.line_to(mx, my);
  155. mesh.line_to(ox[inext], oy[inext]);
  156. mesh.set_corner_color_rgb(0
  157. , hsv_value
  158. , hsv_value
  159. , hsv_value
  160. );
  161. mesh.set_corner_color_rgb(1,
  162. cols[i].x,
  163. cols[i].y,
  164. cols[i].z
  165. );
  166. mesh.set_corner_color_rgb(2,
  167. 0.5 * (cols[i].x + cols[inext].x),
  168. 0.5 * (cols[i].y + cols[inext].y),
  169. 0.5 * (cols[i].z + cols[inext].z)
  170. );
  171. mesh.set_corner_color_rgb(3,
  172. cols[inext].x,
  173. cols[inext].y,
  174. cols[inext].z
  175. );
  176. mesh.end_patch();
  177. }
  178. mesh.set_extend(Cairo.Extend.NONE);
  179. return mesh;
  180. }
  181. public class InputColor3 : InputField
  182. {
  183. public bool _dragging;
  184. public Vector3 _drag_start_rgb;
  185. public int _hs_palette_radius;
  186. public double _hs_lens_radius_scale;
  187. public double _hs_lens_small_radius_scale;
  188. public Gtk.DrawingArea _hs_palette;
  189. public Gtk.Scale _hsv_v_scale;
  190. public Gtk.GestureMultiPress _hsv_v_scale_gesture_click;
  191. public InputDouble _rgb_r;
  192. public InputDouble _rgb_g;
  193. public InputDouble _rgb_b;
  194. public InputDouble _rgb_a;
  195. public InputDouble _hsv_h;
  196. public InputDouble _hsv_s;
  197. public InputDouble _hsv_v;
  198. public InputDouble _hsv_a;
  199. public PropertyGrid _rgb_grid;
  200. public PropertyGrid _hsv_grid;
  201. public InputString _color_string;
  202. public Gtk.Button _picker_button;
  203. public GLib.Object _picker;
  204. public Gtk.GestureMultiPress _gesture_click;
  205. public Gtk.EventControllerMotion _controller_motion;
  206. public Gtk.Box _visual_box;
  207. public Gtk.Box _numeric_box;
  208. public Gtk.Box _input_box;
  209. public Gtk.Box _utils_box;
  210. public Gtk.Box _rgb_box;
  211. public Gtk.Box _hsv_box;
  212. public Gtk.Stack _rgb_hsv_stack;
  213. public Gtk.StackSwitcher _rgb_hsv_switcher;
  214. public Gtk.Popover _popover;
  215. public ColorButton _color_button;
  216. public override void set_inconsistent(bool inconsistent)
  217. {
  218. }
  219. public override bool is_inconsistent()
  220. {
  221. return false;
  222. }
  223. public override GLib.Value union_value()
  224. {
  225. return this.value;
  226. }
  227. public override void set_union_value(GLib.Value v)
  228. {
  229. this.value = (Vector3)v;
  230. }
  231. public Vector3 value
  232. {
  233. get
  234. {
  235. return Vector3(_rgb_r.value, _rgb_g.value, _rgb_b.value);
  236. }
  237. set
  238. {
  239. Vector3 rgb = (Vector3)value;
  240. _rgb_r.value = rgb.x;
  241. _rgb_g.value = rgb.y;
  242. _rgb_b.value = rgb.z;
  243. }
  244. }
  245. public int palette_size_request()
  246. {
  247. double palette_size = 2.0 * _hs_palette_radius;
  248. double lens_size = 2.0 * (_hs_palette_radius * _hs_lens_radius_scale);
  249. return (int)(palette_size + lens_size);
  250. }
  251. public InputColor3()
  252. {
  253. _dragging = false;
  254. _drag_start_rgb = Vector3(1.0, 1.0, 1.0);
  255. _hs_palette_radius = 100;
  256. _hs_lens_radius_scale = 0.1;
  257. _hs_lens_small_radius_scale = _hs_lens_radius_scale * 0.65;
  258. _hs_palette = new Gtk.DrawingArea();
  259. _hs_palette.halign = Gtk.Align.START;
  260. _hs_palette.draw.connect(on_draw_circle);
  261. _hs_palette.size_allocate.connect(on_circle_size_allocate);
  262. int sr = palette_size_request();
  263. _hs_palette.set_size_request(sr, sr);
  264. _gesture_click = new Gtk.GestureMultiPress(_hs_palette);
  265. _gesture_click.set_button(0);
  266. _gesture_click.pressed.connect(on_hs_circle_button_pressed);
  267. _gesture_click.released.connect(on_hs_circle_button_released);
  268. _controller_motion = new Gtk.EventControllerMotion(_hs_palette);
  269. _controller_motion.motion.connect(on_hs_circle_motion);
  270. Gtk.Adjustment adj = new Gtk.Adjustment(0.0, 0.0, 1.0, 0.01, 0.1, 0.0);
  271. _hsv_v_scale = new Gtk.Scale(Gtk.Orientation.VERTICAL, adj);
  272. _hsv_v_scale.get_style_context().add_class("hsv-v-scale");
  273. _hsv_v_scale.halign = Gtk.Align.END;
  274. _hsv_v_scale.draw_value = false;
  275. _hsv_v_scale.set_digits(3);
  276. _hsv_v_scale.halign = Gtk.Align.START;
  277. _hsv_v_scale.value_changed.connect(on_hsv_v_scale_value_changed);
  278. _hsv_v_scale_gesture_click = new Gtk.GestureMultiPress(_hsv_v_scale);
  279. _hsv_v_scale_gesture_click.pressed.connect(on_hsv_v_scale_pressed);
  280. _hsv_v_scale_gesture_click.released.connect(on_hsv_v_scale_released);
  281. _rgb_r = new InputDouble(1.0, 0.0, 1.0);
  282. _rgb_g = new InputDouble(1.0, 0.0, 1.0);
  283. _rgb_b = new InputDouble(1.0, 0.0, 1.0);
  284. _rgb_a = new InputDouble(1.0, 0.0, 1.0);
  285. _rgb_r.value_changed.connect(on_rgb_value_changed);
  286. _rgb_g.value_changed.connect(on_rgb_value_changed);
  287. _rgb_b.value_changed.connect(on_rgb_value_changed);
  288. _hsv_h = new InputDouble(0.0, 0.0, 1.0);
  289. _hsv_s = new InputDouble(0.0, 0.0, 1.0);
  290. _hsv_v = new InputDouble(1.0, 0.0, 1.0);
  291. _hsv_a = new InputDouble(_rgb_a.value, 0.0, 1.0);
  292. _hsv_h.value_changed.connect(on_hsv_value_changed);
  293. _hsv_s.value_changed.connect(on_hsv_value_changed);
  294. _hsv_v.value_changed.connect(on_hsv_value_changed);
  295. _rgb_grid = new PropertyGrid();
  296. _rgb_grid.row_homogeneous = false;
  297. _rgb_grid.label_width_chars = 8;
  298. _rgb_grid.add_row("Red", _rgb_r);
  299. _rgb_grid.add_row("Green", _rgb_g);
  300. _rgb_grid.add_row("Blue", _rgb_b);
  301. _rgb_grid.add_row("Alpha", _rgb_a);
  302. _hsv_grid = new PropertyGrid();
  303. _hsv_grid.row_homogeneous = false;
  304. _hsv_grid.label_width_chars = 8;
  305. _hsv_grid.add_row("Hue", _hsv_h);
  306. _hsv_grid.add_row("Saturation", _hsv_s);
  307. _hsv_grid.add_row("Value", _hsv_v);
  308. _hsv_grid.add_row("Alpha", _hsv_a);
  309. _color_string = new InputString();
  310. _color_string.set_tooltip_text("Lua color code.");
  311. _color_string.value_changed.connect(on_color_string_value_changed);
  312. _picker_button = new Gtk.Button.from_icon_name("color-select-symbolic");
  313. _picker_button.set_tooltip_text("Pick a color from the screen.");
  314. _picker_button.clicked.connect(on_picker_button_clicked);
  315. _picker = gtk_color_picker_new();
  316. _rgb_hsv_stack = new Gtk.Stack();
  317. _rgb_hsv_stack.add_titled(_rgb_grid, "rgb", "RGB");
  318. _rgb_hsv_stack.add_titled(_hsv_grid, "hsv", "HSV");
  319. _rgb_hsv_stack.map.connect(() => {
  320. _rgb_hsv_stack.set_visible_child_name("hsv");
  321. });
  322. _rgb_hsv_switcher = new Gtk.StackSwitcher();
  323. _rgb_hsv_switcher.set_stack(_rgb_hsv_stack);
  324. _visual_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  325. _visual_box.margin = 0;
  326. _visual_box.pack_start(_hs_palette, true, true, 0);
  327. _visual_box.pack_start(_hsv_v_scale, false, false, 0);
  328. _numeric_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 8);
  329. _numeric_box.pack_start(_rgb_hsv_switcher);
  330. _numeric_box.pack_start(_rgb_hsv_stack);
  331. _utils_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 4);
  332. _utils_box.pack_start(_color_string);
  333. _utils_box.pack_start(_picker_button, false);
  334. _input_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 16 /* Avoid slider overlapping buttons. */);
  335. _input_box.margin = 12;
  336. _input_box.pack_start(_visual_box);
  337. _input_box.pack_start(_numeric_box);
  338. _input_box.pack_start(_utils_box);
  339. _popover = new Gtk.Popover(null);
  340. _popover.add(_input_box);
  341. // _popover.has_arrow = false;
  342. _input_box.show_all();
  343. _color_button = new ColorButton();
  344. _color_button.set_tooltip_text("Choose a color.");
  345. _color_button.can_focus = false;
  346. _color_button.set_popover(_popover);
  347. this.value_changed.connect(on_value_changed);
  348. this.add(_color_button);
  349. on_value_changed();
  350. }
  351. public void palette_radius(out double full_radius, out double radius)
  352. {
  353. double w = (double)this._hs_palette.get_allocated_width();
  354. double h = (double)this._hs_palette.get_allocated_height();
  355. full_radius = Math.fmin(w, h) * 0.5;
  356. radius = full_radius - full_radius * _hs_lens_radius_scale;
  357. }
  358. public bool on_draw_circle(Cairo.Context cr)
  359. {
  360. double w = (double)this._hs_palette.get_allocated_width();
  361. double h = (double)this._hs_palette.get_allocated_height();
  362. double cx = w / 2.0;
  363. double cy = h / 2.0;
  364. double full_radius;
  365. double radius;
  366. palette_radius(out full_radius, out radius);
  367. Cairo.MeshPattern mesh = create_circle_mesh(cx, cy, radius, _hsv_v.value);
  368. cr.save();
  369. cr.arc(cx, cy, radius, 0, PI_TWO);
  370. cr.clip();
  371. cr.set_source(mesh);
  372. cr.paint();
  373. cr.restore();
  374. // Draw outline.
  375. cr.set_line_width(1.0);
  376. cr.arc(cx, cy, radius - 0.5, 0, PI_TWO);
  377. cr.set_source_rgb(0.5, 0.5, 0.5);
  378. cr.stroke();
  379. double scale = _dragging ? _hs_lens_radius_scale : _hs_lens_small_radius_scale;
  380. draw_lens(cr, full_radius * scale);
  381. return false;
  382. }
  383. public bool draw_lens(Cairo.Context cr, double radius)
  384. {
  385. Vector2 p = xy_from_hs({ _hsv_h.value, _hsv_s.value });
  386. cr.save();
  387. cr.arc(p.x, p.y, radius, 0, PI_TWO);
  388. cr.clip();
  389. cr.set_source_rgb(_rgb_r.value, _rgb_g.value, _rgb_b.value);
  390. cr.paint();
  391. cr.restore();
  392. // Draw outline.
  393. cr.set_line_width(1.0);
  394. cr.arc(p.x, p.y, radius - 0.5, 0, PI_TWO);
  395. cr.set_source_rgb(0.5, 0.5, 0.5);
  396. cr.stroke();
  397. return false;
  398. }
  399. public void on_circle_size_allocate(Gtk.Allocation allocation)
  400. {
  401. int dia = (int)Math.fmax(1.0, Math.fmin ((double)allocation.width, (double)allocation.height));
  402. _hsv_v_scale.set_size_request(-1, dia);
  403. }
  404. public void on_hsv_v_scale_value_changed()
  405. {
  406. double v = this._hsv_v_scale.get_value();
  407. if (v < 0.0) v = 0.0;
  408. if (v > 1.0) v = 1.0;
  409. _hsv_v.value = 1.0 - v;
  410. _hs_palette.queue_draw();
  411. }
  412. public void on_rgb_value_changed()
  413. {
  414. double h = 0.0;
  415. double s = 0.0;
  416. double v = 1.0;
  417. disconnect_hsv();
  418. rgb_to_hsv(ref h
  419. , ref s
  420. , ref v
  421. , _rgb_r.value
  422. , _rgb_g.value
  423. , _rgb_b.value
  424. );
  425. _hsv_h.value = h;
  426. _hsv_s.value = s;
  427. _hsv_v.value = v;
  428. _hsv_v_scale.set_value(1.0 - _hsv_v.value);
  429. connect_hsv();
  430. _hs_palette.queue_draw();
  431. value_changed(this, (int)!_dragging);
  432. }
  433. public void on_hsv_value_changed()
  434. {
  435. double r = 1.0;
  436. double g = 1.0;
  437. double b = 1.0;
  438. disconnect_rgb();
  439. hsv_to_rgb(ref r
  440. , ref g
  441. , ref b
  442. , _hsv_h.value
  443. , _hsv_s.value
  444. , _hsv_v.value
  445. );
  446. _rgb_r.value = r;
  447. _rgb_g.value = g;
  448. _rgb_b.value = b;
  449. disconnect_hsv();
  450. _hsv_v_scale.set_value(1.0 - _hsv_v.value);
  451. connect_hsv();
  452. connect_rgb();
  453. _hs_palette.queue_draw();
  454. value_changed(this, (int)!_dragging);
  455. }
  456. public void disconnect_rgb()
  457. {
  458. _rgb_r.value_changed.disconnect(on_rgb_value_changed);
  459. _rgb_g.value_changed.disconnect(on_rgb_value_changed);
  460. _rgb_b.value_changed.disconnect(on_rgb_value_changed);
  461. }
  462. public void connect_rgb()
  463. {
  464. _rgb_r.value_changed.connect(on_rgb_value_changed);
  465. _rgb_g.value_changed.connect(on_rgb_value_changed);
  466. _rgb_b.value_changed.connect(on_rgb_value_changed);
  467. }
  468. public void disconnect_hsv()
  469. {
  470. _hsv_h.value_changed.disconnect(on_hsv_value_changed);
  471. _hsv_s.value_changed.disconnect(on_hsv_value_changed);
  472. _hsv_v.value_changed.disconnect(on_hsv_value_changed);
  473. }
  474. public void connect_hsv()
  475. {
  476. _hsv_h.value_changed.connect(on_hsv_value_changed);
  477. _hsv_s.value_changed.connect(on_hsv_value_changed);
  478. _hsv_v.value_changed.connect(on_hsv_value_changed);
  479. }
  480. public void on_value_changed()
  481. {
  482. Vector3 c = this.value;
  483. _color_button.set_color({ c.x, c.y, c.z, 1.0 });
  484. _color_string.value_changed.disconnect(on_color_string_value_changed);
  485. _color_string.value = "Color4(%d, %d, %d, %d)".printf((int)(_rgb_r.value * 255.0)
  486. , (int)(_rgb_g.value * 255.0)
  487. , (int)(_rgb_b.value * 255.0)
  488. , (int)(_rgb_a.value * 255.0)
  489. );
  490. _color_string.value_changed.connect(on_color_string_value_changed);
  491. }
  492. // Returns the hue (x) and saturation (y) of the specified point @a p on a HS circle.
  493. public Vector2 hs_from_xy(Vector2 p)
  494. {
  495. double w = (double)this._hs_palette.get_allocated_width();
  496. double h = (double)this._hs_palette.get_allocated_height();
  497. double radius = w * 0.5;
  498. double cx = (p.x - radius) / w;
  499. double cy = (h - p.y - radius) / h;
  500. Vector2 hs = { 0.0, 0.0 };
  501. hs.x = Math.atan2(cy, cx);
  502. if (hs.x < 0)
  503. hs.x += PI_TWO;
  504. hs.x /= PI_TWO;
  505. hs.y = Vector2(cx, cy).length() * 2.0;
  506. return hs;
  507. }
  508. public Vector2 xy_from_hs(Vector2 hs)
  509. {
  510. double full_radius;
  511. double radius;
  512. palette_radius(out full_radius, out radius);
  513. double cx = Math.cos(PI_TWO * hs.x) * hs.y * radius;
  514. double cy = Math.sin(PI_TWO * hs.x) * hs.y * radius;
  515. cx = cx + full_radius;
  516. cy = full_radius - cy;
  517. return { cx, cy };
  518. }
  519. public void on_hs_circle_button_pressed(int n_press, double x, double y)
  520. {
  521. _dragging = true;
  522. _drag_start_rgb = this.value;
  523. Vector2 hs = hs_from_xy({ x, y });
  524. _hsv_h.value = hs.x;
  525. _hsv_s.value = hs.y;
  526. _hs_palette.get_window().set_cursor(new Gdk.Cursor.from_name(Gdk.Display.get_default(), "none"));
  527. _hs_palette.queue_draw();
  528. }
  529. public void on_hs_circle_button_released(int n_press, double x, double y)
  530. {
  531. double h = 0.0;
  532. double s = 0.0;
  533. double v = 1.0;
  534. Vector3 current_value = this.value;
  535. this.value = _drag_start_rgb;
  536. _dragging = false;
  537. disconnect_rgb();
  538. disconnect_hsv();
  539. this.value = current_value;
  540. rgb_to_hsv(ref h
  541. , ref s
  542. , ref v
  543. , _rgb_r.value
  544. , _rgb_g.value
  545. , _rgb_b.value
  546. );
  547. _hsv_h.value = h;
  548. _hsv_s.value = s;
  549. _hsv_v.value = v;
  550. _hsv_v_scale.set_value(1.0 - _hsv_v.value);
  551. connect_hsv();
  552. connect_rgb();
  553. value_changed(this, (int)!_dragging);
  554. _hs_palette.get_window().set_cursor(new Gdk.Cursor.from_name(Gdk.Display.get_default(), "default"));
  555. _hs_palette.queue_draw();
  556. }
  557. public void on_hs_circle_motion(double x, double y)
  558. {
  559. Vector2 hs = hs_from_xy({ x, y });
  560. _hsv_h.value = hs.x;
  561. _hsv_s.value = hs.y;
  562. }
  563. public void on_color_picked(GLib.Object? object, GLib.AsyncResult result)
  564. {
  565. GLib.Error error = null;
  566. Gdk.RGBA? rgba = gtk_color_picker_pick_finish(_picker, result, ref error);
  567. if (rgba != null)
  568. this.value = Vector3(rgba.red, rgba.green, rgba.blue);
  569. }
  570. public void on_picker_button_clicked()
  571. {
  572. gtk_color_picker_pick(_picker, on_color_picked);
  573. }
  574. public void on_color_string_value_changed()
  575. {
  576. int rgba[4];
  577. if (_color_string.value.scanf("Color4(%d, %d, %d, %d)"
  578. , out rgba[0]
  579. , out rgba[1]
  580. , out rgba[2]
  581. , out rgba[3]
  582. ) == 4) {
  583. _rgb_r.value = rgba[0] / 255.0;
  584. _rgb_g.value = rgba[1] / 255.0;
  585. _rgb_b.value = rgba[2] / 255.0;
  586. _rgb_a.value = rgba[3] / 255.0;
  587. }
  588. }
  589. public void on_hsv_v_scale_pressed(int n_press, double x, double y)
  590. {
  591. _dragging = true;
  592. _drag_start_rgb = this.value;
  593. }
  594. public void on_hsv_v_scale_released(int n_press, double x, double y)
  595. {
  596. double h = 0.0;
  597. double s = 0.0;
  598. double v = 1.0;
  599. Vector3 current_value = this.value;
  600. this.value = _drag_start_rgb;
  601. _dragging = false;
  602. disconnect_rgb();
  603. disconnect_hsv();
  604. this.value = current_value;
  605. rgb_to_hsv(ref h
  606. , ref s
  607. , ref v
  608. , _rgb_r.value
  609. , _rgb_g.value
  610. , _rgb_b.value
  611. );
  612. _hsv_h.value = h;
  613. _hsv_s.value = s;
  614. _hsv_v.value = v;
  615. _hsv_v_scale.set_value(1.0 - _hsv_v.value);
  616. connect_hsv();
  617. connect_rgb();
  618. value_changed(this, (int)!_dragging);
  619. }
  620. }
  621. } /* namespace Crown */