1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.util.colorparse;
26 
27 ///
28 ulong hex2ulong( string hex )
29 {
30     import std.string;
31 
32     enum tbl = [ '0':  0, '1':  1, '2':  2, '3':  3, 
33                  '4':  4, '5':  5, '6':  6, '7':  7, 
34                  '8':  8, '9':  9, 'A': 10, 'B': 11, 
35                  'C': 12, 'D': 13, 'E': 14, 'F': 15 ];
36 
37     ulong ret = 0;
38     size_t i = 0;
39     auto uHex = toUpper( hex );
40     foreach_reverse( c; uHex )
41         ret += tbl[c] << (4 * i++);
42 
43     return ret;
44 }
45 
46 ///
47 T[4] parseColorStr(T=float)( string cstr, real norm=1 )
48 {
49     if( cstr[0] == '#' )
50         cstr = cstr[ 1 .. $ ];
51     else if( cstr[0] == '0' && cstr[1] == 'x' )
52         cstr = cstr[ 2 .. $ ];
53     else
54         throw new Exception( "unsupported hex color string (format): '" ~ cstr ~ "'" );
55 
56     ulong shortWrite = 0;
57     bool alpha = false;
58     switch( cstr.length )
59     {
60         case 1:
61             shortWrite = 2;
62             break;
63         case 2:
64             shortWrite = 3;
65             alpha = true;
66             break;
67         case 3:
68             shortWrite = 1;
69             break;
70         case 4:
71             shortWrite = 1;
72             alpha = true;
73             break;
74         case 6: break;
75         case 8:
76             alpha = true;
77             break;
78         default: 
79             throw new Exception( "unsupported hex color string (length): '" ~ cstr ~ "'" );
80     }
81 
82     if( shortWrite == 1 )
83     {
84         string buf;
85 
86         foreach( c; cstr )
87             buf ~= [c,c];
88 
89         cstr = buf;
90     }
91     else if( shortWrite >= 2 )
92     {
93         string buf;
94 
95         buf ~= [ cstr[0], cstr[0], cstr[0], cstr[0], cstr[0], cstr[0] ];
96 
97         if( shortWrite == 3 )
98             buf ~= [ cstr[1], cstr[1] ];
99 
100         cstr = buf;
101     }
102 
103     ulong val = hex2ulong( cstr );
104 
105     enum compbits = byte.sizeof * 8;
106     enum compmask = 0xFF;
107 
108     auto k = norm / cast(real)(compmask);
109     auto cr = cast(T)( ( ( val >> ( ( 2 + alpha ) * compbits ) ) & compmask ) * k );
110     auto cg = cast(T)( ( ( val >> ( ( 1 + alpha ) * compbits ) ) & compmask ) * k );
111     auto cb = cast(T)( ( ( val >> ( ( 0 + alpha ) * compbits ) ) & compmask ) * k );
112     auto ca = cast(T)( alpha ? ( val & compmask ) * k : norm );
113 
114     return [ cr, cg, cb, ca ];
115 }
116 
117 ///
118 unittest
119 {
120     assert( [1.0f,0,1,0] == parseColorStr( "#FF00FF00" ) );
121     assert( [1.0f,0,1,1] == parseColorStr( "#FF00FF" ) );
122     assert( [1.0f,0,1,0] == parseColorStr( "#F0F0" ) );
123     assert( [1.0f,0,1,1] == parseColorStr( "#F0F" ) );
124     assert( [1.0f,1,1,0] == parseColorStr( "#F0" ) );
125     assert( [1.0f,1,1,1] == parseColorStr( "#F" ) );
126     assert( [0.0f,0,0,1] == parseColorStr( "#0" ) );
127     assert( [1.0f,1,1,1] == parseColorStr( "0xF" ) );
128     assert( [0.0f,0,0,1] == parseColorStr( "0x0" ) );
129 
130     assert( [255,15,255,255] == parseColorStr!ubyte( "#FF0FFF", 255 ) );
131 }