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 ulong hex2ulong( string hex )
28 {
29     import std.string;
30 
31     enum tbl = [ '0':  0, '1':  1, '2':  2, '3':  3, 
32                  '4':  4, '5':  5, '6':  6, '7':  7, 
33                  '8':  8, '9':  9, 'A': 10, 'B': 11, 
34                  'C': 12, 'D': 13, 'E': 14, 'F': 15 ];
35 
36     ulong ret = 0;
37     size_t i = 0;
38     auto uHex = toUpper( hex );
39     foreach_reverse( c; uHex )
40         ret += tbl[c] << (4 * i++);
41 
42     return ret;
43 }
44 
45 T[4] parseColorStr(T=float)( string cstr, real norm=1 )
46 {
47     if( cstr[0] == '#' )
48         cstr = cstr[ 1 .. $ ];
49     else if( cstr[0] == '0' && cstr[1] == 'x' )
50         cstr = cstr[ 2 .. $ ];
51     else
52         throw new Exception( "unsupported hex color string (format): '" ~ cstr ~ "'" );
53 
54     ulong shortWrite = 0;
55     bool alpha = false;
56     switch( cstr.length )
57     {
58         case 1:
59             shortWrite = 2;
60             break;
61         case 2:
62             shortWrite = 3;
63             alpha = true;
64             break;
65         case 3:
66             shortWrite = 1;
67             break;
68         case 4:
69             shortWrite = 1;
70             alpha = true;
71             break;
72         case 6: break;
73         case 8:
74             alpha = true;
75             break;
76         default: 
77             throw new Exception( "unsupported hex color string (length): '" ~ cstr ~ "'" );
78     }
79 
80     if( shortWrite == 1 )
81     {
82         string buf;
83 
84         foreach( c; cstr )
85             buf ~= [c,c];
86 
87         cstr = buf;
88     }
89     else if( shortWrite >= 2 )
90     {
91         string buf;
92 
93         buf ~= [ cstr[0], cstr[0], cstr[0], cstr[0], cstr[0], cstr[0] ];
94 
95         if( shortWrite == 3 )
96             buf ~= [ cstr[1], cstr[1] ];
97 
98         cstr = buf;
99     }
100 
101     ulong val = hex2ulong( cstr );
102 
103     enum compbits = byte.sizeof * 8;
104     enum compmask = 0xFF;
105 
106     auto k = norm / cast(real)(compmask);
107     auto cr = cast(T)( ( ( val >> ( ( 2 + alpha ) * compbits ) ) & compmask ) * k );
108     auto cg = cast(T)( ( ( val >> ( ( 1 + alpha ) * compbits ) ) & compmask ) * k );
109     auto cb = cast(T)( ( ( val >> ( ( 0 + alpha ) * compbits ) ) & compmask ) * k );
110     auto ca = cast(T)( alpha ? ( val & compmask ) * k : norm );
111 
112     return [ cr, cg, cb, ca ];
113 }
114 
115 unittest
116 {
117     assert( [1.0f,0,1,0] == parseColorStr( "#FF00FF00" ) );
118     assert( [1.0f,0,1,1] == parseColorStr( "#FF00FF" ) );
119     assert( [1.0f,0,1,0] == parseColorStr( "#F0F0" ) );
120     assert( [1.0f,0,1,1] == parseColorStr( "#F0F" ) );
121     assert( [1.0f,1,1,0] == parseColorStr( "#F0" ) );
122     assert( [1.0f,1,1,1] == parseColorStr( "#F" ) );
123     assert( [0.0f,0,0,1] == parseColorStr( "#0" ) );
124     assert( [1.0f,1,1,1] == parseColorStr( "0xF" ) );
125     assert( [0.0f,0,0,1] == parseColorStr( "0x0" ) );
126 
127     assert( [255,15,255,255] == parseColorStr!ubyte( "#FF0FFF", 255 ) );
128 }