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 the SVG parser and the SVG rendering code 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).replace ("#", ""); 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.char_count () == 6) { 45 color.scanf ("%x", out c); 46 parsed.r = (uint8)((c & 0xFF0000) >> 16) / 254.0; 47 parsed.g = (uint8)((c & 0x00FF00) >> 8)/ 254.0; 48 parsed.b = (uint8)(c & 0x0000FF) / 254.0; 49 } else if (color.char_count () == 3) { 50 color.scanf ("%x", out c); 51 parsed.r = (uint8)(((c & 0xF00) >> 4) | ((c & 0xF00) >> 8)) / 254.0; 52 parsed.g = (uint8)((c & 0x0F0) | ((c & 0x0F0) >> 4)) / 254.0; 53 parsed.b = (uint8)(((c & 0x00F) << 4) | (c & 0x00F)) / 254.0; 54 } else if (color.index_of ("%") > -1) { 55 color = color.replace ("rgb", ""); 56 color = color.replace (" ", ""); 57 color = color.replace ("\t", ""); 58 color = color.replace ("%", ""); 59 arguments = color.split (","); 60 61 return_val_if_fail (arguments.length == 3, parsed); 62 arguments[0].scanf ("%lf", out parsed.r); 63 arguments[1].scanf ("%lf", out parsed.g); 64 arguments[2].scanf ("%lf", out parsed.b); 65 } else if (color.index_of ("rgb") > -1) { 66 color = color.replace ("rgb", ""); 67 color = color.replace (" ", ""); 68 color = color.replace ("\t", ""); 69 arguments = color.split (","); 70 71 return_val_if_fail (arguments.length == 3, parsed); 72 73 int r, g, b; 74 arguments[0].scanf ("%d", out r); 75 parsed.r = r / 254.0; 76 77 arguments[1].scanf ("%d", out g); 78 parsed.g = g / 254.0; 79 80 arguments[2].scanf ("%d", out b); 81 parsed.b = b / 254.0; 82 } else { 83 warning ("Unknown color type: " + color); 84 } 85 86 87 return parsed; 88 } 89 90 public string to_rgb_hex () { 91 StringBuilder rgb = new StringBuilder (); 92 rgb.append ("#"); 93 rgb.append_printf ("%x", (int) Math.rint (r * 254)); 94 rgb.append_printf ("%x", (int) Math.rint (g * 254)); 95 rgb.append_printf ("%x", (int) Math.rint (b * 254)); 96 return rgb.str; 97 } 98 99 public string to_string () { 100 StringBuilder rgba = new StringBuilder (); 101 rgba.append (to_rgb_hex ()); 102 rgba.append_printf ("%x", (int) Math.rint (a * 254)); 103 return rgba.str; 104 } 105 106 107 public Color copy () { 108 return new Color (r, g, b, a); 109 } 110 111 } 112 113 } 114