1 /*
2  *	colour1.cc
3  *	getcolour()
4  *	AYM 1998-01-27
5  */
6 
7 
8 /*
9 This file is part of Yadex.
10 
11 Yadex incorporates code from DEU 5.21 that was put in the public domain in
12 1994 by Rapha�l Quinet and Brendon Wyber.
13 
14 The rest of Yadex is Copyright � 1997-2003 Andr� Majorel and others.
15 
16 This program is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any later
19 version.
20 
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License along with
26 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
27 Place, Suite 330, Boston, MA 02111-1307, USA.
28 */
29 
30 
31 #include "yadex.h"
32 #include "rgb.h"
33 
34 
35 #define RGB_DIGITS 2  /* R, G and B are 8-bit wide each */
36 
37 
38 /*
39  *	getcolour
40  *	Decode an "rgb:<r>/<g>/<b>" colour specification
41  *	Returns :
42  *	  0    OK
43  *	  <>0  malformed colour specification
44  */
getcolour(const char * s,rgb_c * rgb)45 int getcolour (const char *s, rgb_c *rgb)
46 {
47   int i;
48   int digit;
49   int rdigits;
50   int gdigits;
51   int bdigits;
52   unsigned r;
53   unsigned g;
54   unsigned b;
55   int globaldigits;
56 
57   if (strncmp (s, "rgb:", 4))
58     return 1;
59 
60   for (i = 4, r = 0, rdigits = 0; (digit = hextoi (s[i])) >= 0; i++, rdigits++)
61     r = (r << 4) | digit;
62   if (s[i++] != '/')
63     return 2;
64 
65   for (g = 0, gdigits = 0; (digit = hextoi (s[i])) >= 0; i++, gdigits++)
66     g = (g << 4) | digit;
67   if (s[i++] != '/')
68     return 3;
69 
70   for (b = 0, bdigits = 0; (digit = hextoi (s[i])) >= 0; i++, bdigits++)
71     b = (b << 4) | digit;
72   if (s[i++] != '\0')
73     return 4;
74 
75   // Force to 8 bits (RGB_DIGITS hex digits) by scaling up or down
76   globaldigits = rdigits;
77   globaldigits = y_max (globaldigits, gdigits);
78   globaldigits = y_max (globaldigits, bdigits);
79   for (; globaldigits < RGB_DIGITS; globaldigits++)
80   {
81     r <<= 4;
82     g <<= 4;
83     b <<= 4;
84   }
85   for (; globaldigits > RGB_DIGITS; globaldigits--)
86   {
87     r >>= 4;
88     g >>= 4;
89     b >>= 4;
90   }
91   rgb->set (r, g, b);
92   return 0;
93 }
94 
95 
96