Commit 6ad0b7a
Hsb to rgb
Changed files (6)
modifiedlibbirdfont/Color.valamodifiedlibbirdfont/ColorPicker.valamodifiedlibbirdfont/MoveTool.valamodifiedlibbirdfont/Path.valamodifiedlibbirdfont/StrokeTool.valamodifiedlibbirdfont/ThemeTools.vala
Diff
@@ -1,16 +1,24 @@-/*- Copyright (C) 2014 Johan Mattsson-- This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 3 of the - License, or (at your option) any later version.-- This library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details.-*/+/* Copyright (C) 1999 The Free Software Foundation+ *+ * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code)+ * Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+)+ * Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+)+ *+ * This library is free software; you can redistribute it and/or+ * modify it under the terms of the GNU Lesser General Public+ * License as published by the Free Software Foundation; either+ * version 2 of the License, or (at your option) any later version.+ *+ * This library is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU+ * Lesser General Public License for more details.+ *+ * You should have received a copy of the GNU Lesser General Public+ * License along with this library; if not, write to the+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,+ * Boston, MA 02111-1307, USA.+ */ namespace BirdFont { @@ -27,48 +35,73 @@ public class Color { this.a = a; } - public Color.hsba (double h, double s, double b, double a) {- double q, p;+ public Color.hsba (double h, double s, double v, double a) {+ double hue, saturation, value;+ double f, p, q, t; - if (s == 0) {- r = b;- g = b;- this.b = b;- } else {- q = b < 0.5 ? b * (1 + s) : b + s - b * s;- p = 2 * b - q;- r = hue_to_rgb(p, q, h + 1f/3);- g = hue_to_rgb(p, q, h);- this.b = hue_to_rgb(p, q, h - 1f/3);- }- this.a = a;- } - static double hue_to_rgb (double p, double q, double t){- if(t < 0) {- t += 1;- }- - if(t > 1) {- t -= 1;- }- - if(t < 1.0 / 6) {- return p + (q - p) * 6 * t;- } - - if(t < 1.0 / 2) {- return q;- }- - if(t < 2.0 / 3) {- return p + (q - p) * (2f/3 - t) * 6;+ if (s == 0.0) {+ r = v;+ g = v;+ b = v;+ } else {+ hue = h * 6.0;+ saturation = s;+ value = v;++ if (hue == 6.0) {+ hue = 0.0;+ }++ f = hue - (int) hue;+ p = value * (1.0 - saturation);+ q = value * (1.0 - saturation * f);+ t = value * (1.0 - saturation * (1.0 - f));++ switch ((int) hue) {+ case 0:+ r = value;+ g = t;+ b = p;+ break;++ case 1:+ r = q;+ g = value;+ b = p;+ break;++ case 2:+ r = p;+ g = value;+ b = t;+ break;++ case 3:+ r = p;+ g = q;+ b = value;+ break;++ case 4:+ r = t;+ g = p;+ b = value;+ break;++ case 5:+ r = value;+ g = p;+ b = q;+ break;++ default:+ assert_not_reached ();+ } }- - return p; }- + public static Color black () { return new Color (0, 0, 0, 1); }@@ -23,20 +23,27 @@ public class ColorPicker : Tool { double b = 0; double a = 1; - public signal void color_updated ();+ public signal void fill_color_updated ();+ public signal void stroke_color_updated (); bool update_color = false; public double bar_height; int selected_bar = 0; + public bool has_stroke_color = false;+ bool stroke_selected = false;+ + Color stroke_color = new Color (0, 0, 0, 1);+ Color fill_color = new Color (0, 0, 0, 1);+ public ColorPicker (string tooltip = "") { base (null, tooltip); bar_height = 22 * Toolbox.get_scale (); h = 5 * bar_height; - color_updated.connect (() => {+ stroke_color_updated.connect (() => { redraw (); GlyphCanvas.redraw (); });@@ -62,6 +69,11 @@ public class ColorPicker : Tool { }); } + public void set_color (Color c) {+ c.get_hsba (out hue, out s, out b, out a);+ print (@"hue $hue s $s b $b a $a\n");+ }+ public void set_color_from_pointer (double tx) { if (tx > Toolbox.allocation_width) { tx = Toolbox.allocation_width;@@ -79,15 +91,35 @@ public class ColorPicker : Tool { b = (double) tx / Toolbox.allocation_width; } else if (selected_bar == 3) { a = (double) tx / Toolbox.allocation_width;- }+ } else if (selected_bar == 4) {+ if (has_stroke_color) {+ stroke_selected = tx > Toolbox.allocation_width / 2.0; - color_updated ();+ if (stroke_selected) {+ set_color (stroke_color);+ } else {+ set_color (fill_color);+ }+ }+ }+ + if (has_stroke_color && stroke_selected) {+ stroke_color = new Color.hsba (hue, s, b, a);+ stroke_color_updated ();+ } else {+ fill_color = new Color.hsba (hue, s, b, a);+ fill_color_updated ();+ } } - public Color get_color () {- return new Color.hsba (hue, s, b, a);+ public Color get_stroke_color () {+ return stroke_color; } + public Color get_fill_color () {+ return fill_color;+ }+ public override void draw_tool (Context cr, double px, double py) { draw_bars (cr, px, py); draw_dial (cr, px, py, 0, hue);@@ -103,14 +135,14 @@ public class ColorPicker : Tool { double y = this.y - py; for (double p = 0; p < 1; p += step) {- c = new Color.hsba (p, 1, 0.5, 1);+ c = new Color.hsba (p, 1, 1, 1); cr.save (); cr.set_source_rgba (c.r, c.g, c.b, c.a); cr.rectangle (p * Toolbox.allocation_width, y, scale, bar_height); cr.fill (); cr.restore (); - c = new Color.hsba (hue, p, 0.5, 1);+ c = new Color.hsba (hue, p, 1, 1); cr.save (); cr.set_source_rgba (c.r, c.g, c.b, c.a); cr.rectangle (p * Toolbox.allocation_width, y + bar_height, scale, bar_height);@@ -123,21 +155,54 @@ public class ColorPicker : Tool { cr.rectangle (p * Toolbox.allocation_width, y + 2 * bar_height, scale, bar_height); cr.fill (); cr.restore ();- + c = new Color.hsba (hue, s, b, p); cr.save (); cr.set_source_rgba (c.r, c.g, c.b, c.a); cr.rectangle (p * Toolbox.allocation_width, y + 3 * bar_height, scale, bar_height); cr.fill (); cr.restore ();+ + if (!has_stroke_color) {+ cr.save ();+ cr.set_source_rgba (c.r, c.g, c.b, c.a);+ cr.rectangle (0, y + 4 * bar_height, Toolbox.allocation_width, bar_height);+ cr.fill ();+ cr.restore ();+ } else {+ double cw = Toolbox.allocation_width / 2.0 - 2 * scale;+ + cr.save ();+ cr.set_source_rgba (fill_color.r, fill_color.g, fill_color.b, fill_color.a);+ cr.rectangle (0, y + 4 * bar_height, cw, bar_height);+ cr.fill ();+ cr.restore ();++ cr.save ();+ cr.set_source_rgba (stroke_color.r, stroke_color.g, stroke_color.b, stroke_color.a);+ cr.rectangle (cw + 4 * scale, y + 4 * bar_height, cw, bar_height);+ cr.fill ();+ cr.restore ();+ + if (has_stroke_color) {+ if (stroke_selected) {+ cr.save ();+ Theme.color (cr, "Tool Foreground");+ cr.set_line_width (1);+ cr.rectangle (cw + 4 * scale, y + 4 * bar_height, cw, bar_height);+ cr.stroke ();+ cr.restore ();+ } else {+ cr.save ();+ Theme.color (cr, "Tool Foreground");+ cr.set_line_width (1);+ cr.rectangle (0, y + 4 * bar_height, cw, bar_height);+ cr.stroke ();+ cr.restore ();+ }+ }+ } }- - c = new Color.hsba (hue, s, b, a);- cr.save ();- cr.set_source_rgba (c.r, c.g, c.b, c.a);- cr.rectangle (0, y + 4 * bar_height, Toolbox.allocation_width, bar_height);- cr.fill ();- cr.restore (); } void draw_dial (Context cr, double px, double py, int bar_index, double val) {@@ -128,9 +128,6 @@ public class MoveTool : Tool { last_x = x; last_y = y; - GlyphCanvas.redraw ();- PenTool.reset_stroke ();- update_selection_boundaries (); if (glyph.active_paths.size > 0) {@@ -138,6 +135,9 @@ public class MoveTool : Tool { } BirdFont.get_current_font ().touch ();++ GlyphCanvas.redraw ();+ PenTool.reset_stroke (); } public void release (int b, int x, int y) {@@ -763,7 +763,12 @@ public class Path { if (color != null) { new_path.color = ((!) color).copy (); }++ if (stroke_color != null) {+ new_path.stroke_color = ((!) stroke_color).copy ();+ } + new_path.fill = fill; new_path.edit = edit; new_path.open = open; new_path.stroke = stroke;@@ -851,6 +856,10 @@ public class Path { } } + if (insides > 0 && is_filled ()) {+ return true;+ }+ if (insides % 2 == 1) { return true; }@@ -2305,11 +2305,7 @@ public class StrokeTool : Tool { Path merged = new Path (); PathList paths = new PathList (); - if (!p.is_open () && p.is_filled ()) {- side1.close ();- side1.update_region_boundaries ();- paths.add (side1);- } else if (!p.is_open () && !p.is_filled ()) {+ if (!p.is_open ()) { side1.update_region_boundaries (); paths.add (side1); side2.update_region_boundaries ();@@ -30,8 +30,9 @@ public class ThemeTools : ToolCollection { Expander color_tools = new Expander (t_("Color")); color_picker = new ColorPicker (); - color_picker.color_updated.connect (() => {- ThemeTab.get_instance ().color_updated (color_picker.get_color ());+ color_picker.fill_color_updated.connect (() => {+ Color c = color_picker.get_fill_color ();+ ThemeTab.get_instance ().color_updated (c); }); color_tools.add_tool (color_picker);