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

Revisions

View the latest version of libsvgbird/Color.vala.
Move hsva color to libsvgbird
1 /* 2 Copyright (C) 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 namespace SvgBird { 16 17 public class Color { 18 public double r; 19 public double g; 20 public double b; 21 public double a; 22 23 public Color (double r, double g, double b, double a) { 24 this.r = r; 25 this.g = g; 26 this.b = b; 27 this.a = a; 28 } 29 30 public static Color? parse (string? svg_color) { 31 if (svg_color == null) { 32 return null; 33 } 34 35 string color = (!) svg_color; 36 uint32 c; 37 string[] arguments; 38 Color parsed = new Color (0, 0, 0, 1); 39 40 if (color == "none") { 41 return null; 42 } 43 44 if (!color.has_prefix ("#")) { 45 color = get_hex_for_name (color); 46 } 47 48 color = color.replace ("#", ""); 49 50 if (color.char_count () == 6) { 51 color.scanf ("%x", out c); 52 parsed.r = (uint8)((c & 0xFF0000) >> 16) / 254.0; 53 parsed.g = (uint8)((c & 0x00FF00) >> 8)/ 254.0; 54 parsed.b = (uint8)(c & 0x0000FF) / 254.0; 55 } else if (color.char_count () == 3) { 56 color.scanf ("%x", out c); 57 parsed.r = (uint8)(((c & 0xF00) >> 4) | ((c & 0xF00) >> 8)) / 254.0; 58 parsed.g = (uint8)((c & 0x0F0) | ((c & 0x0F0) >> 4)) / 254.0; 59 parsed.b = (uint8)(((c & 0x00F) << 4) | (c & 0x00F)) / 254.0; 60 } else if (color.index_of ("%") > -1) { 61 color = color.replace ("rgb", ""); 62 color = color.replace (" ", ""); 63 color = color.replace ("\t", ""); 64 color = color.replace ("%", ""); 65 arguments = color.split (","); 66 67 return_val_if_fail (arguments.length == 3, parsed); 68 arguments[0].scanf ("%lf", out parsed.r); 69 arguments[1].scanf ("%lf", out parsed.g); 70 arguments[2].scanf ("%lf", out parsed.b); 71 } else if (color.index_of ("rgb") > -1) { 72 color = color.replace ("rgb", ""); 73 color = color.replace (" ", ""); 74 color = color.replace ("\t", ""); 75 arguments = color.split (","); 76 77 return_val_if_fail (arguments.length == 3, parsed); 78 79 int r, g, b; 80 arguments[0].scanf ("%d", out r); 81 parsed.r = r / 254.0; 82 83 arguments[1].scanf ("%d", out g); 84 parsed.g = g / 254.0; 85 86 arguments[2].scanf ("%d", out b); 87 parsed.b = b / 254.0; 88 } else { 89 warning ("Unknown color type: " + color); 90 } 91 92 return parsed; 93 } 94 95 public string to_rgb_hex () { 96 StringBuilder rgb = new StringBuilder (); 97 rgb.append ("#"); 98 rgb.append_printf ("%02x", (int) Math.rint (r * 254)); 99 rgb.append_printf ("%02x", (int) Math.rint (g * 254)); 100 rgb.append_printf ("%02x", (int) Math.rint (b * 254)); 101 return rgb.str; 102 } 103 104 public string to_string () { 105 StringBuilder rgba = new StringBuilder (); 106 rgba.append (to_rgb_hex ()); 107 rgba.append_printf ("%x", (int) Math.rint (a * 254)); 108 return rgba.str; 109 } 110 111 public Color copy () { 112 return new Color (r, g, b, a); 113 } 114 115 public static string get_hex_for_name (string name) { 116 string color = name.down (); 117 118 if (color == "black") return "#000000"; 119 if (color == "silver") return "#C0C0C0"; 120 if (color == "gray") return "#808080"; 121 if (color == "maroon") return "#800000"; 122 if (color == "red") return "#FF0000"; 123 if (color == "purple") return "#800080"; 124 if (color == "white") return "#FFFFFF"; 125 if (color == "fuchsia") return "#FF00FF"; 126 if (color == "green") return "#008000"; 127 if (color == "lime") return "#00FF00"; 128 if (color == "olive") return "#808000"; 129 if (color == "yellow") return "#FFFF00"; 130 if (color == "navy") return "#000080"; 131 if (color == "blue") return "#0000FF"; 132 if (color == "teal") return "#008080"; 133 if (color == "aqua") return "#00FFFF"; 134 if (color == "aliceblue") return "#f0f8ff"; 135 if (color == "antiquewhite") return "#faebd7"; 136 if (color == "aqua") return "#00ffff"; 137 if (color == "aquamarine") return "#7fffd4"; 138 if (color == "azure") return "#f0ffff"; 139 if (color == "beige") return "#f5f5dc"; 140 if (color == "bisque") return "#ffe4c4"; 141 if (color == "black") return "#000000"; 142 if (color == "blanchedalmond") return "#ffebcd"; 143 if (color == "blue") return "#0000ff"; 144 if (color == "blueviolet") return "#8a2be2"; 145 if (color == "brown") return "#a52a2a"; 146 if (color == "burlywood") return "#deb887"; 147 if (color == "cadetblue") return "#5f9ea0"; 148 if (color == "chartreuse") return "#7fff00"; 149 if (color == "chocolate") return "#d2691e"; 150 if (color == "coral") return "#ff7f50"; 151 if (color == "cornflowerblue") return "#6495ed"; 152 if (color == "cornsilk") return "#fff8dc"; 153 if (color == "crimson") return "#dc143c"; 154 if (color == "cyan") return "#00ffff"; 155 if (color == "darkblue") return "#00008b"; 156 if (color == "darkcyan") return "#008b8b"; 157 if (color == "darkgoldenrod") return "#b8860b"; 158 if (color == "darkgray") return "#a9a9a9"; 159 if (color == "darkgreen") return "#006400"; 160 if (color == "darkgrey") return "#a9a9a9"; 161 if (color == "darkkhaki") return "#bdb76b"; 162 if (color == "darkmagenta") return "#8b008b"; 163 if (color == "darkolivegreen") return "#556b2f"; 164 if (color == "darkorange") return "#ff8c00"; 165 if (color == "darkorchid") return "#9932cc"; 166 if (color == "darkred") return "#8b0000"; 167 if (color == "darksalmon") return "#e9967a"; 168 if (color == "darkseagreen") return "#8fbc8f"; 169 if (color == "darkslateblue") return "#483d8b"; 170 if (color == "darkslategray") return "#2f4f4f"; 171 if (color == "darkslategrey") return "#2f4f4f"; 172 if (color == "darkturquoise") return "#00ced1"; 173 if (color == "darkviolet") return "#9400d3"; 174 if (color == "deeppink") return "#ff1493"; 175 if (color == "deepskyblue") return "#00bfff"; 176 if (color == "dimgray") return "#696969"; 177 if (color == "dimgrey") return "#696969"; 178 if (color == "dodgerblue") return "#1e90ff"; 179 if (color == "firebrick") return "#b22222"; 180 if (color == "floralwhite") return "#fffaf0"; 181 if (color == "forestgreen") return "#228b22"; 182 if (color == "fuchsia") return "#ff00ff"; 183 if (color == "gainsboro") return "#dcdcdc"; 184 if (color == "ghostwhite") return "#f8f8ff"; 185 if (color == "gold") return "#ffd700"; 186 if (color == "goldenrod") return "#daa520"; 187 if (color == "gray") return "#808080"; 188 if (color == "green") return "#008000"; 189 if (color == "greenyellow") return "#adff2f"; 190 if (color == "grey") return "#808080"; 191 if (color == "honeydew") return "#f0fff0"; 192 if (color == "hotpink") return "#ff69b4"; 193 if (color == "indianred") return "#cd5c5c"; 194 if (color == "indigo") return "#4b0082"; 195 if (color == "ivory") return "#fffff0"; 196 if (color == "khaki") return "#f0e68c"; 197 if (color == "lavender") return "#e6e6fa"; 198 if (color == "lavenderblush") return "#fff0f5"; 199 if (color == "lawngreen") return "#7cfc00"; 200 if (color == "lemonchiffon") return "#fffacd"; 201 if (color == "lightblue") return "#add8e6"; 202 if (color == "lightcoral") return "#f08080"; 203 if (color == "lightcyan") return "#e0ffff"; 204 if (color == "lightgoldenrodyellow") return "#fafad2"; 205 if (color == "lightgray") return "#d3d3d3"; 206 if (color == "lightgreen") return "#90ee90"; 207 if (color == "lightgrey") return "#d3d3d3"; 208 if (color == "lightpink") return "#ffb6c1"; 209 if (color == "lightsalmon") return "#ffa07a"; 210 if (color == "lightseagreen") return "#20b2aa"; 211 if (color == "lightskyblue") return "#87cefa"; 212 if (color == "lightslategray") return "#778899"; 213 if (color == "lightslategrey") return "#778899"; 214 if (color == "lightsteelblue") return "#b0c4de"; 215 if (color == "lightyellow") return "#ffffe0"; 216 if (color == "lime") return "#00ff00"; 217 if (color == "limegreen") return "#32cd32"; 218 if (color == "linen") return "#faf0e6"; 219 if (color == "magenta") return "#ff00ff"; 220 if (color == "maroon") return "#800000"; 221 if (color == "mediumaquamarine") return "#66cdaa"; 222 if (color == "mediumblue") return "#0000cd"; 223 if (color == "mediumorchid") return "#ba55d3"; 224 if (color == "mediumpurple") return "#9370db"; 225 if (color == "mediumseagreen") return "#3cb371"; 226 if (color == "mediumslateblue") return "#7b68ee"; 227 if (color == "mediumspringgreen") return "#00fa9a"; 228 if (color == "mediumturquoise") return "#48d1cc"; 229 if (color == "mediumvioletred") return "#c71585"; 230 if (color == "midnightblue") return "#191970"; 231 if (color == "mintcream") return "#f5fffa"; 232 if (color == "mistyrose") return "#ffe4e1"; 233 if (color == "moccasin") return "#ffe4b5"; 234 if (color == "navajowhite") return "#ffdead"; 235 if (color == "navy") return "#000080"; 236 if (color == "oldlace") return "#fdf5e6"; 237 if (color == "olive") return "#808000"; 238 if (color == "olivedrab") return "#6b8e23"; 239 if (color == "orange") return "#ffa500"; 240 if (color == "orangered") return "#ff4500"; 241 if (color == "orchid") return "#da70d6"; 242 if (color == "palegoldenrod") return "#eee8aa"; 243 if (color == "palegreen") return "#98fb98"; 244 if (color == "paleturquoise") return "#afeeee"; 245 if (color == "palevioletred") return "#db7093"; 246 if (color == "papayawhip") return "#ffefd5"; 247 if (color == "peachpuff") return "#ffdab9"; 248 if (color == "peru") return "#cd853f"; 249 if (color == "pink") return "#ffc0cb"; 250 if (color == "plum") return "#dda0dd"; 251 if (color == "powderblue") return "#b0e0e6"; 252 if (color == "purple") return "#800080"; 253 if (color == "red") return "#ff0000"; 254 if (color == "rosybrown") return "#bc8f8f"; 255 if (color == "royalblue") return "#4169e1"; 256 if (color == "saddlebrown") return "#8b4513"; 257 if (color == "salmon") return "#fa8072"; 258 if (color == "sandybrown") return "#f4a460"; 259 if (color == "seagreen") return "#2e8b57"; 260 if (color == "seashell") return "#fff5ee"; 261 if (color == "sienna") return "#a0522d"; 262 if (color == "silver") return "#c0c0c0"; 263 if (color == "skyblue") return "#87ceeb"; 264 if (color == "slateblue") return "#6a5acd"; 265 if (color == "slategray") return "#708090"; 266 if (color == "slategrey") return "#708090"; 267 if (color == "snow") return "#fffafa"; 268 if (color == "springgreen") return "#00ff7f"; 269 if (color == "steelblue") return "#4682b4"; 270 if (color == "tan") return "#d2b48c"; 271 if (color == "teal") return "#008080"; 272 if (color == "thistle") return "#d8bfd8"; 273 if (color == "tomato") return "#ff6347"; 274 if (color == "turquoise") return "#40e0d0"; 275 if (color == "violet") return "#ee82ee"; 276 if (color == "wheat") return "#f5deb3"; 277 if (color == "white") return "#ffffff"; 278 if (color == "whitesmoke") return "#f5f5f5"; 279 if (color == "yellow") return "#ffff00"; 280 if (color == "yellowgreen") return "#9acd32"; 281 282 return "#000000"; 283 } 284 } 285 286 } 287