The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Color.vala in libbirdfont

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/Color.vala.
Move hsva color to libsvgbird
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 : SvgBird.Color { 26 public Color (double r, double g, double b, double a) { 27 base (r, g, b, a); 28 } 29 30 public Color.create_copy (SvgBird.Color color) { 31 base (color.r, color.g, color.b, color.a); 32 } 33 34 public Color.hsba (double h, double s, double v, double a) { 35 double hue, saturation, value; 36 double f, p, q, t; 37 double r, g, b; 38 39 if (s == 0.0) { 40 r = v; 41 g = v; 42 b = v; 43 } else { 44 hue = h * 6.0; 45 saturation = s; 46 value = v; 47 48 if (hue == 6.0) { 49 hue = 0.0; 50 } 51 52 f = hue - (int) hue; 53 p = value * (1.0 - saturation); 54 q = value * (1.0 - saturation * f); 55 t = value * (1.0 - saturation * (1.0 - f)); 56 57 switch ((int) hue) { 58 case 0: 59 r = value; 60 g = t; 61 b = p; 62 break; 63 64 case 1: 65 r = q; 66 g = value; 67 b = p; 68 break; 69 70 case 2: 71 r = p; 72 g = value; 73 b = t; 74 break; 75 76 case 3: 77 r = p; 78 g = q; 79 b = value; 80 break; 81 82 case 4: 83 r = t; 84 g = p; 85 b = value; 86 break; 87 88 case 5: 89 r = value; 90 g = p; 91 b = q; 92 break; 93 94 default: 95 assert_not_reached (); 96 } 97 } 98 99 base (r, g, b, a); 100 } 101 102 public void to_hsva (out double h, out double s, out double v, out double a) { 103 double red, green, blue; 104 double min, max; 105 double delta; 106 107 a = this.a; 108 109 red = r; 110 green = g; 111 blue = b; 112 113 h = 0.0; 114 115 if (red > green) { 116 if (red > blue) 117 max = red; 118 else 119 max = blue; 120 121 if (green < blue) 122 min = green; 123 else 124 min = blue; 125 } else { 126 if (green > blue) 127 max = green; 128 else 129 max = blue; 130 131 if (red < blue) 132 min = red; 133 else 134 min = blue; 135 } 136 137 v = max; 138 139 if (max != 0.0) 140 s = (max - min) / max; 141 else 142 s = 0.0; 143 144 if (s == 0.0) 145 h = 0.0; 146 else { 147 delta = max - min; 148 149 if (red == max) 150 h = (green - blue) / delta; 151 else if (green == max) 152 h = 2 + (blue - red) / delta; 153 else if (blue == max) 154 h = 4 + (red - green) / delta; 155 156 h /= 6.0; 157 158 if (h < 0.0) 159 h += 1.0; 160 else if (h > 1.0) 161 h -= 1.0; 162 } 163 } 164 165 public static Color black () { 166 return new Color (0, 0, 0, 1); 167 } 168 169 public static Color red () { 170 return new Color (1, 0, 0, 1); 171 } 172 173 public static Color green () { 174 return new Color (0, 1, 0, 1); 175 } 176 177 public static Color blue () { 178 return new Color (0, 0, 1, 1); 179 } 180 181 public static Color yellow () { 182 return new Color (222.0 / 255, 203.0 / 255, 43 / 255.0, 1); 183 } 184 185 public static Color brown () { 186 return new Color (160.0 / 255, 90.0 / 255, 44.0 / 255, 1); 187 } 188 189 public static Color pink () { 190 return new Color (247.0 / 255, 27.0 / 255, 113 / 255.0, 1); 191 } 192 193 public static Color white () { 194 return new Color (1, 1, 1, 1); 195 } 196 197 public static Color grey () { 198 return new Color (0.5, 0.5, 0.5, 1); 199 } 200 201 public static Color magenta () { 202 return new Color (103.0 / 255, 33.0 / 255, 120.0 / 255, 1); 203 } 204 205 public new Color copy () { 206 return new Color (r, g, b, a); 207 } 208 } 209 210 } 211