The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Color.vala in libbirdfont/Svg

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/Svg/Color.vala.
Linear gradients and matrix tranform on gradients
1 /* Copyright (C) 1999 The Free Software Foundation 2 * 3 * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code) 4 * Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+) 5 * Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+) 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the 19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 * Boston, MA 02111-1307, USA. 21 */ 22 23 namespace BirdFont { 24 25 public class Color { 26 public double r; 27 public double g; 28 public double b; 29 public double a; 30 31 public Color (double r, double g, double b, double a) { 32 this.r = r; 33 this.g = g; 34 this.b = b; 35 this.a = a; 36 } 37 38 public Color.hsba (double h, double s, double v, double a) { 39 double hue, saturation, value; 40 double f, p, q, t; 41 42 this.a = a; 43 44 if (s == 0.0) { 45 r = v; 46 g = v; 47 b = v; 48 } else { 49 hue = h * 6.0; 50 saturation = s; 51 value = v; 52 53 if (hue == 6.0) { 54 hue = 0.0; 55 } 56 57 f = hue - (int) hue; 58 p = value * (1.0 - saturation); 59 q = value * (1.0 - saturation * f); 60 t = value * (1.0 - saturation * (1.0 - f)); 61 62 switch ((int) hue) { 63 case 0: 64 r = value; 65 g = t; 66 b = p; 67 break; 68 69 case 1: 70 r = q; 71 g = value; 72 b = p; 73 break; 74 75 case 2: 76 r = p; 77 g = value; 78 b = t; 79 break; 80 81 case 3: 82 r = p; 83 g = q; 84 b = value; 85 break; 86 87 case 4: 88 r = t; 89 g = p; 90 b = value; 91 break; 92 93 case 5: 94 r = value; 95 g = p; 96 b = q; 97 break; 98 99 default: 100 assert_not_reached (); 101 } 102 } 103 } 104 105 public static Color? parse (string? svg_color) { 106 if (svg_color == null) { 107 return null; 108 } 109 110 string color = ((!) svg_color).replace ("#", ""); 111 uint32 c; 112 string[] arguments; 113 Color parsed = black (); 114 115 if (color == "none") { 116 return null; 117 } 118 119 if (color.char_count () == 6) { 120 color.scanf ("%x", out c); 121 parsed.r = (uint8)((c & 0xFF0000) >> 16) / 254.0; 122 parsed.g = (uint8)((c & 0x00FF00) >> 8)/ 254.0; 123 parsed.b = (uint8)(c & 0x0000FF) / 254.0; 124 } else if (color.char_count () == 3) { 125 color.scanf ("%x", out c); 126 parsed.r = (uint8)(((c & 0xF00) >> 4) | ((c & 0xF00) >> 8)) / 254.0; 127 parsed.g = (uint8)((c & 0x0F0) | ((c & 0x0F0) >> 4)) / 254.0; 128 parsed.b = (uint8)(((c & 0x00F) << 4) | (c & 0x00F)) / 254.0; 129 } else if (color.index_of ("%") > -1) { 130 color = color.replace ("rgb", ""); 131 color = color.replace (" ", ""); 132 color = color.replace ("\t", ""); 133 color = color.replace ("%", ""); 134 arguments = color.split (","); 135 136 return_val_if_fail (arguments.length == 3, parsed); 137 arguments[0].scanf ("%lf", out parsed.r); 138 arguments[1].scanf ("%lf", out parsed.g); 139 arguments[2].scanf ("%lf", out parsed.b); 140 } else if (color.index_of ("rgb") > -1) { 141 color = color.replace ("rgb", ""); 142 color = color.replace (" ", ""); 143 color = color.replace ("\t", ""); 144 arguments = color.split (","); 145 146 return_val_if_fail (arguments.length == 3, parsed); 147 148 int r, g, b; 149 arguments[0].scanf ("%d", out r); 150 parsed.r = r / 254.0; 151 152 arguments[1].scanf ("%d", out g); 153 parsed.g = g / 254.0; 154 155 arguments[2].scanf ("%d", out b); 156 parsed.b = b / 254.0; 157 } else { 158 warning ("Unknown color type: " + color); 159 } 160 161 162 return parsed; 163 } 164 165 public void to_hsva (out double h, out double s, out double v, out double a) { 166 double red, green, blue; 167 double min, max; 168 double delta; 169 170 a = this.a; 171 172 red = r; 173 green = g; 174 blue = b; 175 176 h = 0.0; 177 178 if (red > green) { 179 if (red > blue) 180 max = red; 181 else 182 max = blue; 183 184 if (green < blue) 185 min = green; 186 else 187 min = blue; 188 } else { 189 if (green > blue) 190 max = green; 191 else 192 max = blue; 193 194 if (red < blue) 195 min = red; 196 else 197 min = blue; 198 } 199 200 v = max; 201 202 if (max != 0.0) 203 s = (max - min) / max; 204 else 205 s = 0.0; 206 207 if (s == 0.0) 208 h = 0.0; 209 else { 210 delta = max - min; 211 212 if (red == max) 213 h = (green - blue) / delta; 214 else if (green == max) 215 h = 2 + (blue - red) / delta; 216 else if (blue == max) 217 h = 4 + (red - green) / delta; 218 219 h /= 6.0; 220 221 if (h < 0.0) 222 h += 1.0; 223 else if (h > 1.0) 224 h -= 1.0; 225 } 226 } 227 228 public static Color black () { 229 return new Color (0, 0, 0, 1); 230 } 231 232 public static Color red () { 233 return new Color (1, 0, 0, 1); 234 } 235 236 public static Color green () { 237 return new Color (0, 1, 0, 1); 238 } 239 240 public static Color blue () { 241 return new Color (0, 0, 1, 1); 242 } 243 244 public static Color yellow () { 245 return new Color (222.0 / 255, 203.0 / 255, 43 / 255.0, 1); 246 } 247 248 public static Color brown () { 249 return new Color (160.0 / 255, 90.0 / 255, 44.0 / 255, 1); 250 } 251 252 public static Color pink () { 253 return new Color (247.0 / 255, 27.0 / 255, 113 / 255.0, 1); 254 } 255 256 public static Color white () { 257 return new Color (1, 1, 1, 1); 258 } 259 260 public static Color grey () { 261 return new Color (0.5, 0.5, 0.5, 1); 262 } 263 264 public static Color magenta () { 265 return new Color (103.0 / 255, 33.0 / 255, 120.0 / 255, 1); 266 } 267 268 public string to_string () { 269 string alpha = Font.to_hex_code ((unichar) Math.rint (a * 254)); 270 return @"$(to_rgb_hex ())" + alpha; 271 } 272 273 public Color copy () { 274 return new Color (r, g, b, a); 275 } 276 277 public string to_rgb_hex () { 278 string s = "#"; 279 s += Font.to_hex_code ((unichar) Math.rint (r * 254)); 280 s += Font.to_hex_code ((unichar) Math.rint (g * 254)); 281 s += Font.to_hex_code ((unichar) Math.rint (b * 254)); 282 return s; 283 } 284 } 285 286 } 287