The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Color.vala in /libsvgbird

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
Circle boundaries heads/master
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 * Johan Mattsson (adapted to BirdFont) 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the 20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21 * Boston, MA 02111-1307, USA. 22 */ 23 24 namespace SvgBird { 25 26 public class Color { 27 public double r; 28 public double g; 29 public double b; 30 public double a; 31 32 public Color (double r, double g, double b, double a) { 33 this.r = r; 34 this.g = g; 35 this.b = b; 36 this.a = a; 37 } 38 39 public Color.create_copy (SvgBird.Color color) { 40 this.r = color.r; 41 this.g = color.g; 42 this.b = color.b; 43 this.a = color.a; 44 } 45 46 public Color.hsva (double h, double s, double v, double a) { 47 double hue, saturation, value; 48 double f, p, q, t; 49 double r, g, b; 50 51 if (s == 0.0) { 52 r = v; 53 g = v; 54 b = v; 55 } else { 56 hue = h * 6.0; 57 saturation = s; 58 value = v; 59 60 if (hue == 6.0) { 61 hue = 0.0; 62 } 63 64 f = hue - (int) hue; 65 p = value * (1.0 - saturation); 66 q = value * (1.0 - saturation * f); 67 t = value * (1.0 - saturation * (1.0 - f)); 68 69 switch ((int) hue) { 70 case 0: 71 r = value; 72 g = t; 73 b = p; 74 break; 75 76 case 1: 77 r = q; 78 g = value; 79 b = p; 80 break; 81 82 case 2: 83 r = p; 84 g = value; 85 b = t; 86 break; 87 88 case 3: 89 r = p; 90 g = q; 91 b = value; 92 break; 93 94 case 4: 95 r = t; 96 g = p; 97 b = value; 98 break; 99 100 case 5: 101 r = value; 102 g = p; 103 b = q; 104 break; 105 106 default: 107 assert_not_reached (); 108 } 109 } 110 111 this.r = r; 112 this.g = g; 113 this.b = b; 114 this.a = a; 115 } 116 117 public void to_hsva (out double h, out double s, out double v, out double a) { 118 double red, green, blue; 119 double min, max; 120 double delta; 121 122 a = this.a; 123 124 red = r; 125 green = g; 126 blue = b; 127 128 h = 0.0; 129 130 if (red > green) { 131 if (red > blue) 132 max = red; 133 else 134 max = blue; 135 136 if (green < blue) 137 min = green; 138 else 139 min = blue; 140 } else { 141 if (green > blue) 142 max = green; 143 else 144 max = blue; 145 146 if (red < blue) 147 min = red; 148 else 149 min = blue; 150 } 151 152 v = max; 153 154 if (max != 0.0) 155 s = (max - min) / max; 156 else 157 s = 0.0; 158 159 if (s == 0.0) 160 h = 0.0; 161 else { 162 delta = max - min; 163 164 if (red == max) 165 h = (green - blue) / delta; 166 else if (green == max) 167 h = 2 + (blue - red) / delta; 168 else if (blue == max) 169 h = 4 + (red - green) / delta; 170 171 h /= 6.0; 172 173 if (h < 0.0) 174 h += 1.0; 175 else if (h > 1.0) 176 h -= 1.0; 177 } 178 } 179 180 public static Color? parse (string? svg_color) { 181 if (svg_color == null) { 182 return null; 183 } 184 185 string color = (!) svg_color; 186 uint32 c; 187 string[] arguments; 188 Color parsed = new Color (0, 0, 0, 1); 189 190 if (color == "none") { 191 return null; 192 } 193 194 if (!color.has_prefix ("#")) { 195 color = get_hex_for_name (color); 196 } 197 198 color = color.replace ("#", ""); 199 200 if (color.char_count () == 6) { 201 color.scanf ("%x", out c); 202 parsed.r = (uint8)((c & 0xFF0000) >> 16) / 254.0; 203 parsed.g = (uint8)((c & 0x00FF00) >> 8)/ 254.0; 204 parsed.b = (uint8)(c & 0x0000FF) / 254.0; 205 } else if (color.char_count () == 3) { 206 color.scanf ("%x", out c); 207 parsed.r = (uint8)(((c & 0xF00) >> 4) | ((c & 0xF00) >> 8)) / 254.0; 208 parsed.g = (uint8)((c & 0x0F0) | ((c & 0x0F0) >> 4)) / 254.0; 209 parsed.b = (uint8)(((c & 0x00F) << 4) | (c & 0x00F)) / 254.0; 210 } else if (color.index_of ("%") > -1) { 211 color = color.replace ("rgb", ""); 212 color = color.replace (" ", ""); 213 color = color.replace ("\t", ""); 214 color = color.replace ("%", ""); 215 arguments = color.split (","); 216 217 return_val_if_fail (arguments.length == 3, parsed); 218 arguments[0].scanf ("%lf", out parsed.r); 219 arguments[1].scanf ("%lf", out parsed.g); 220 arguments[2].scanf ("%lf", out parsed.b); 221 } else if (color.index_of ("rgb") > -1) { 222 color = color.replace ("rgb", ""); 223 color = color.replace (" ", ""); 224 color = color.replace ("\t", ""); 225 arguments = color.split (","); 226 227 return_val_if_fail (arguments.length == 3, parsed); 228 229 int r, g, b; 230 arguments[0].scanf ("%d", out r); 231 parsed.r = r / 254.0; 232 233 arguments[1].scanf ("%d", out g); 234 parsed.g = g / 254.0; 235 236 arguments[2].scanf ("%d", out b); 237 parsed.b = b / 254.0; 238 } else { 239 warning ("Unknown color type: " + color); 240 } 241 242 return parsed; 243 } 244 245 public string to_rgb_hex () { 246 StringBuilder rgb = new StringBuilder (); 247 rgb.append ("#"); 248 rgb.append_printf ("%02x", (int) Math.rint (r * 254)); 249 rgb.append_printf ("%02x", (int) Math.rint (g * 254)); 250 rgb.append_printf ("%02x", (int) Math.rint (b * 254)); 251 return rgb.str; 252 } 253 254 public string to_string () { 255 StringBuilder rgba = new StringBuilder (); 256 rgba.append (to_rgb_hex ()); 257 rgba.append_printf ("%x", (int) Math.rint (a * 254)); 258 return rgba.str; 259 } 260 261 public Color copy () { 262 return new Color (r, g, b, a); 263 } 264 265 public static string get_hex_for_name (string name) { 266 string color = name.down (); 267 268 if (color == "black") return "#000000"; 269 if (color == "silver") return "#C0C0C0"; 270 if (color == "gray") return "#808080"; 271 if (color == "maroon") return "#800000"; 272 if (color == "red") return "#FF0000"; 273 if (color == "purple") return "#800080"; 274 if (color == "white") return "#FFFFFF"; 275 if (color == "fuchsia") return "#FF00FF"; 276 if (color == "green") return "#008000"; 277 if (color == "lime") return "#00FF00"; 278 if (color == "olive") return "#808000"; 279 if (color == "yellow") return "#FFFF00"; 280 if (color == "navy") return "#000080"; 281 if (color == "blue") return "#0000FF"; 282 if (color == "teal") return "#008080"; 283 if (color == "aqua") return "#00FFFF"; 284 if (color == "aliceblue") return "#f0f8ff"; 285 if (color == "antiquewhite") return "#faebd7"; 286 if (color == "aqua") return "#00ffff"; 287 if (color == "aquamarine") return "#7fffd4"; 288 if (color == "azure") return "#f0ffff"; 289 if (color == "beige") return "#f5f5dc"; 290 if (color == "bisque") return "#ffe4c4"; 291 if (color == "black") return "#000000"; 292 if (color == "blanchedalmond") return "#ffebcd"; 293 if (color == "blue") return "#0000ff"; 294 if (color == "blueviolet") return "#8a2be2"; 295 if (color == "brown") return "#a52a2a"; 296 if (color == "burlywood") return "#deb887"; 297 if (color == "cadetblue") return "#5f9ea0"; 298 if (color == "chartreuse") return "#7fff00"; 299 if (color == "chocolate") return "#d2691e"; 300 if (color == "coral") return "#ff7f50"; 301 if (color == "cornflowerblue") return "#6495ed"; 302 if (color == "cornsilk") return "#fff8dc"; 303 if (color == "crimson") return "#dc143c"; 304 if (color == "cyan") return "#00ffff"; 305 if (color == "darkblue") return "#00008b"; 306 if (color == "darkcyan") return "#008b8b"; 307 if (color == "darkgoldenrod") return "#b8860b"; 308 if (color == "darkgray") return "#a9a9a9"; 309 if (color == "darkgreen") return "#006400"; 310 if (color == "darkgrey") return "#a9a9a9"; 311 if (color == "darkkhaki") return "#bdb76b"; 312 if (color == "darkmagenta") return "#8b008b"; 313 if (color == "darkolivegreen") return "#556b2f"; 314 if (color == "darkorange") return "#ff8c00"; 315 if (color == "darkorchid") return "#9932cc"; 316 if (color == "darkred") return "#8b0000"; 317 if (color == "darksalmon") return "#e9967a"; 318 if (color == "darkseagreen") return "#8fbc8f"; 319 if (color == "darkslateblue") return "#483d8b"; 320 if (color == "darkslategray") return "#2f4f4f"; 321 if (color == "darkslategrey") return "#2f4f4f"; 322 if (color == "darkturquoise") return "#00ced1"; 323 if (color == "darkviolet") return "#9400d3"; 324 if (color == "deeppink") return "#ff1493"; 325 if (color == "deepskyblue") return "#00bfff"; 326 if (color == "dimgray") return "#696969"; 327 if (color == "dimgrey") return "#696969"; 328 if (color == "dodgerblue") return "#1e90ff"; 329 if (color == "firebrick") return "#b22222"; 330 if (color == "floralwhite") return "#fffaf0"; 331 if (color == "forestgreen") return "#228b22"; 332 if (color == "fuchsia") return "#ff00ff"; 333 if (color == "gainsboro") return "#dcdcdc"; 334 if (color == "ghostwhite") return "#f8f8ff"; 335 if (color == "gold") return "#ffd700"; 336 if (color == "goldenrod") return "#daa520"; 337 if (color == "gray") return "#808080"; 338 if (color == "green") return "#008000"; 339 if (color == "greenyellow") return "#adff2f"; 340 if (color == "grey") return "#808080"; 341 if (color == "honeydew") return "#f0fff0"; 342 if (color == "hotpink") return "#ff69b4"; 343 if (color == "indianred") return "#cd5c5c"; 344 if (color == "indigo") return "#4b0082"; 345 if (color == "ivory") return "#fffff0"; 346 if (color == "khaki") return "#f0e68c"; 347 if (color == "lavender") return "#e6e6fa"; 348 if (color == "lavenderblush") return "#fff0f5"; 349 if (color == "lawngreen") return "#7cfc00"; 350 if (color == "lemonchiffon") return "#fffacd"; 351 if (color == "lightblue") return "#add8e6"; 352 if (color == "lightcoral") return "#f08080"; 353 if (color == "lightcyan") return "#e0ffff"; 354 if (color == "lightgoldenrodyellow") return "#fafad2"; 355 if (color == "lightgray") return "#d3d3d3"; 356 if (color == "lightgreen") return "#90ee90"; 357 if (color == "lightgrey") return "#d3d3d3"; 358 if (color == "lightpink") return "#ffb6c1"; 359 if (color == "lightsalmon") return "#ffa07a"; 360 if (color == "lightseagreen") return "#20b2aa"; 361 if (color == "lightskyblue") return "#87cefa"; 362 if (color == "lightslategray") return "#778899"; 363 if (color == "lightslategrey") return "#778899"; 364 if (color == "lightsteelblue") return "#b0c4de"; 365 if (color == "lightyellow") return "#ffffe0"; 366 if (color == "lime") return "#00ff00"; 367 if (color == "limegreen") return "#32cd32"; 368 if (color == "linen") return "#faf0e6"; 369 if (color == "magenta") return "#ff00ff"; 370 if (color == "maroon") return "#800000"; 371 if (color == "mediumaquamarine") return "#66cdaa"; 372 if (color == "mediumblue") return "#0000cd"; 373 if (color == "mediumorchid") return "#ba55d3"; 374 if (color == "mediumpurple") return "#9370db"; 375 if (color == "mediumseagreen") return "#3cb371"; 376 if (color == "mediumslateblue") return "#7b68ee"; 377 if (color == "mediumspringgreen") return "#00fa9a"; 378 if (color == "mediumturquoise") return "#48d1cc"; 379 if (color == "mediumvioletred") return "#c71585"; 380 if (color == "midnightblue") return "#191970"; 381 if (color == "mintcream") return "#f5fffa"; 382 if (color == "mistyrose") return "#ffe4e1"; 383 if (color == "moccasin") return "#ffe4b5"; 384 if (color == "navajowhite") return "#ffdead"; 385 if (color == "navy") return "#000080"; 386 if (color == "oldlace") return "#fdf5e6"; 387 if (color == "olive") return "#808000"; 388 if (color == "olivedrab") return "#6b8e23"; 389 if (color == "orange") return "#ffa500"; 390 if (color == "orangered") return "#ff4500"; 391 if (color == "orchid") return "#da70d6"; 392 if (color == "palegoldenrod") return "#eee8aa"; 393 if (color == "palegreen") return "#98fb98"; 394 if (color == "paleturquoise") return "#afeeee"; 395 if (color == "palevioletred") return "#db7093"; 396 if (color == "papayawhip") return "#ffefd5"; 397 if (color == "peachpuff") return "#ffdab9"; 398 if (color == "peru") return "#cd853f"; 399 if (color == "pink") return "#ffc0cb"; 400 if (color == "plum") return "#dda0dd"; 401 if (color == "powderblue") return "#b0e0e6"; 402 if (color == "purple") return "#800080"; 403 if (color == "red") return "#ff0000"; 404 if (color == "rosybrown") return "#bc8f8f"; 405 if (color == "royalblue") return "#4169e1"; 406 if (color == "saddlebrown") return "#8b4513"; 407 if (color == "salmon") return "#fa8072"; 408 if (color == "sandybrown") return "#f4a460"; 409 if (color == "seagreen") return "#2e8b57"; 410 if (color == "seashell") return "#fff5ee"; 411 if (color == "sienna") return "#a0522d"; 412 if (color == "silver") return "#c0c0c0"; 413 if (color == "skyblue") return "#87ceeb"; 414 if (color == "slateblue") return "#6a5acd"; 415 if (color == "slategray") return "#708090"; 416 if (color == "slategrey") return "#708090"; 417 if (color == "snow") return "#fffafa"; 418 if (color == "springgreen") return "#00ff7f"; 419 if (color == "steelblue") return "#4682b4"; 420 if (color == "tan") return "#d2b48c"; 421 if (color == "teal") return "#008080"; 422 if (color == "thistle") return "#d8bfd8"; 423 if (color == "tomato") return "#ff6347"; 424 if (color == "turquoise") return "#40e0d0"; 425 if (color == "violet") return "#ee82ee"; 426 if (color == "wheat") return "#f5deb3"; 427 if (color == "white") return "#ffffff"; 428 if (color == "whitesmoke") return "#f5f5f5"; 429 if (color == "yellow") return "#ffff00"; 430 if (color == "yellowgreen") return "#9acd32"; 431 432 return "#000000"; 433 } 434 } 435 436 } 437