3 /* <groff_src_dir>/src/libs/libgroff/color.cpp
5 Copyright (C) 2001-2014 Free Software Foundation, Inc.
6 Written by Gaius Mulley <gaius@glam.ac.uk>
8 This file is part of groff.
10 groff is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
37 static inline unsigned int
38 min(const unsigned int a, const unsigned int b)
46 color *color::free_list = 0;
48 void *color::operator new(size_t n)
50 assert(n == sizeof(color));
52 const int BLOCK = 128;
53 free_list = (color *)new char[sizeof(color)*BLOCK];
54 for (int i = 0; i < BLOCK - 1; i++)
55 free_list[i].next = free_list + i + 1;
56 free_list[BLOCK-1].next = 0;
59 free_list = (color *)(free_list->next);
64 void color::operator delete(void *p)
67 ((color *)p)->next = free_list;
68 free_list = (color *)p;
72 color::color(const color * const c)
76 components[0] = c->components[0];
77 components[1] = c->components[1];
78 components[2] = c->components[2];
79 components[3] = c->components[3];
86 int color::operator==(const color & c) const
88 if (scheme != c.scheme)
94 if (Red != c.Red || Green != c.Green || Blue != c.Blue)
98 if (Cyan != c.Cyan || Magenta != c.Magenta
99 || Yellow != c.Yellow || Black != c.Black)
107 if (Cyan != c.Cyan || Magenta != c.Magenta || Yellow != c.Yellow)
114 int color::operator!=(const color & c) const
116 return !(*this == c);
119 color_scheme color::get_components(unsigned int *c) const
122 if (sizeof (c) < sizeof (unsigned int) * 4)
123 fatal("argument is not big enough to store 4 color components");
125 c[0] = components[0];
126 c[1] = components[1];
127 c[2] = components[2];
128 c[3] = components[3];
132 void color::set_default()
137 // (0, 0, 0) is black
139 void color::set_rgb(const unsigned int r, const unsigned int g,
140 const unsigned int b)
143 Red = min(MAX_COLOR_VAL, r);
144 Green = min(MAX_COLOR_VAL, g);
145 Blue = min(MAX_COLOR_VAL, b);
148 // (0, 0, 0) is white
150 void color::set_cmy(const unsigned int c, const unsigned int m,
151 const unsigned int y)
154 Cyan = min(MAX_COLOR_VAL, c);
155 Magenta = min(MAX_COLOR_VAL, m);
156 Yellow = min(MAX_COLOR_VAL, y);
159 // (0, 0, 0, 0) is white
161 void color::set_cmyk(const unsigned int c, const unsigned int m,
162 const unsigned int y, const unsigned int k)
165 Cyan = min(MAX_COLOR_VAL, c);
166 Magenta = min(MAX_COLOR_VAL, m);
167 Yellow = min(MAX_COLOR_VAL, y);
168 Black = min(MAX_COLOR_VAL, k);
173 void color::set_gray(const unsigned int g)
176 Gray = min(MAX_COLOR_VAL, g);
180 * atoh - computes the decimal value of a hexadecimal number string.
181 * `length' characters of `s' are read. Returns 1 if successful.
184 static int atoh(unsigned int *result,
185 const char * const s, const size_t length)
188 unsigned int val = 0;
189 while ((i < length) && csxdigit(s[i])) {
191 val = val*0x10 + (s[i]-'0');
192 else if (csupper(s[i]))
193 val = val*0x10 + (s[i]-'A') + 10;
195 val = val*0x10 + (s[i]-'a') + 10;
205 * read_encoding - set color from a hexadecimal color string.
207 * Use color scheme `cs' to parse `n' color components from string `s'.
208 * Returns 1 if successful.
211 int color::read_encoding(const color_scheme cs, const char * const s,
214 size_t hex_length = 2;
216 char *p = (char *) s;
222 for (size_t i = 0; i < n; i++) {
223 if (!atoh(&(components[i]), p, hex_length))
226 components[i] *= 0x101; // scale up -- 0xff should become 0xffff
232 int color::read_rgb(const char * const s)
234 return read_encoding(RGB, s, 3);
237 int color::read_cmy(const char * const s)
239 return read_encoding(CMY, s, 3);
242 int color::read_cmyk(const char * const s)
244 return read_encoding(CMYK, s, 4);
247 int color::read_gray(const char * const s)
249 return read_encoding(GRAY, s, 1);
253 color::get_rgb(unsigned int *r, unsigned int *g, unsigned int *b) const
262 *r = MAX_COLOR_VAL - Cyan;
263 *g = MAX_COLOR_VAL - Magenta;
264 *b = MAX_COLOR_VAL - Yellow;
269 Cyan * (MAX_COLOR_VAL - Black) / MAX_COLOR_VAL + Black);
272 Magenta * (MAX_COLOR_VAL - Black) / MAX_COLOR_VAL + Black);
275 Yellow * (MAX_COLOR_VAL - Black) / MAX_COLOR_VAL + Black);
287 color::get_cmy(unsigned int *c, unsigned int *m, unsigned int *y) const
291 *c = MAX_COLOR_VAL - Red;
292 *m = MAX_COLOR_VAL - Green;
293 *y = MAX_COLOR_VAL - Blue;
301 *c = min(MAX_COLOR_VAL,
302 Cyan * (MAX_COLOR_VAL - Black) / MAX_COLOR_VAL + Black);
303 *m = min(MAX_COLOR_VAL,
304 Magenta * (MAX_COLOR_VAL - Black) / MAX_COLOR_VAL + Black);
305 *y = min(MAX_COLOR_VAL,
306 Yellow * (MAX_COLOR_VAL - Black) / MAX_COLOR_VAL + Black);
309 *c = *m = *y = MAX_COLOR_VAL - Gray;
317 void color::get_cmyk(unsigned int *c, unsigned int *m,
318 unsigned int *y, unsigned int *k) const
322 *k = min(MAX_COLOR_VAL - Red,
323 min(MAX_COLOR_VAL - Green, MAX_COLOR_VAL - Blue));
324 if (MAX_COLOR_VAL == *k) {
330 *c = (MAX_COLOR_VAL * (MAX_COLOR_VAL - Red - *k))
331 / (MAX_COLOR_VAL - *k);
332 *m = (MAX_COLOR_VAL * (MAX_COLOR_VAL - Green - *k))
333 / (MAX_COLOR_VAL - *k);
334 *y = (MAX_COLOR_VAL * (MAX_COLOR_VAL - Blue - *k))
335 / (MAX_COLOR_VAL - *k);
339 *k = min(Cyan, min(Magenta, Yellow));
340 if (MAX_COLOR_VAL == *k) {
346 *c = (MAX_COLOR_VAL * (Cyan - *k)) / (MAX_COLOR_VAL - *k);
347 *m = (MAX_COLOR_VAL * (Magenta - *k)) / (MAX_COLOR_VAL - *k);
348 *y = (MAX_COLOR_VAL * (Yellow - *k)) / (MAX_COLOR_VAL - *k);
359 *k = MAX_COLOR_VAL - Gray;
367 // we use `0.222r + 0.707g + 0.071b' (this is the ITU standard)
368 // as an approximation for gray
370 void color::get_gray(unsigned int *g) const
374 *g = (222*Red + 707*Green + 71*Blue) / 1000;
377 *g = MAX_COLOR_VAL - (222*Cyan + 707*Magenta + 71*Yellow) / 1000;
380 *g = (MAX_COLOR_VAL - (222*Cyan + 707*Magenta + 71*Yellow) / 1000)
381 * (MAX_COLOR_VAL - Black);
392 char *color::print_color()
394 char *s = new char[30];
397 sprintf(s, "default");
400 sprintf(s, "rgb %.2ff %.2ff %.2ff",
401 double(Red) / MAX_COLOR_VAL,
402 double(Green) / MAX_COLOR_VAL,
403 double(Blue) / MAX_COLOR_VAL);
406 sprintf(s, "cmy %.2ff %.2ff %.2ff",
407 double(Cyan) / MAX_COLOR_VAL,
408 double(Magenta) / MAX_COLOR_VAL,
409 double(Yellow) / MAX_COLOR_VAL);
412 sprintf(s, "cmyk %.2ff %.2ff %.2ff %.2ff",
413 double(Cyan) / MAX_COLOR_VAL,
414 double(Magenta) / MAX_COLOR_VAL,
415 double(Yellow) / MAX_COLOR_VAL,
416 double(Black) / MAX_COLOR_VAL);
419 sprintf(s, "gray %.2ff",
420 double(Gray) / MAX_COLOR_VAL);