|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "BMCONV.H" |
|
19 #include "RGB.H" |
|
20 |
|
21 extern TRgb* color256Palette; |
|
22 extern char* color256InversePalette; |
|
23 |
|
24 TRgb* color256Palette = NULL; |
|
25 char* color256InversePalette = NULL; |
|
26 |
|
27 TRgb::TRgb() |
|
28 : iRed(255),iGreen(255),iBlue(255),iSpare(0) |
|
29 /** Constructs default TRgb with all 3 colour components set to 255. */ |
|
30 {} |
|
31 |
|
32 TRgb::TRgb(long unsigned int val) |
|
33 : iRed((unsigned char)(val&0xff)),iGreen((unsigned char)((val>>8)&0xff)),iBlue((unsigned char)((val>>16)&0xff)),iSpare(0) |
|
34 {} |
|
35 |
|
36 TRgb::TRgb(int r,int g,int b) |
|
37 : iRed((unsigned char)r),iGreen((unsigned char)g),iBlue((unsigned char)b),iSpare(0) |
|
38 /** Constructs a TRgb from its three component colours. |
|
39 |
|
40 Each colour has a value between 0 and 255. |
|
41 |
|
42 @param aRed Red component of the colour (0 - 255). |
|
43 @param aGreen Green component of the colour (0 - 255). |
|
44 @param aBlue Blue component of the colour (0 - 255). */ |
|
45 {} |
|
46 |
|
47 TRgb &TRgb::operator=(const TRgb &col) |
|
48 { |
|
49 iRed=col.iRed; |
|
50 iGreen=col.iGreen; |
|
51 iBlue=col.iBlue; |
|
52 return(*this); |
|
53 } |
|
54 |
|
55 int TRgb::operator==(const TRgb &col) |
|
56 { |
|
57 return(iRed==col.iRed && iGreen==col.iGreen && iBlue==col.iBlue); |
|
58 } |
|
59 |
|
60 int TRgb::Difference(const TRgb& col) const |
|
61 { |
|
62 return abs(iRed-col.iRed) + abs(iGreen-col.iGreen) + abs(iBlue-col.iBlue); |
|
63 } |
|
64 |
|
65 int TRgb::Gray2() const |
|
66 /** Gets a 2 level grayscale value from this TRgb. |
|
67 |
|
68 @return Equivalent 2 level grayscale value. The greyscale value is 0 or 1, |
|
69 and is calculated using c=(2*r+5*g+b)/1024. Note that the return value is |
|
70 rounded down to the nearest integer. */ |
|
71 { |
|
72 return Gray256() / 128; |
|
73 } |
|
74 |
|
75 int TRgb::Gray4() const |
|
76 /** Gets a 4 level grayscale value from this TRgb. |
|
77 |
|
78 @return Equivalent 4 level grayscale value. The greyscale value is calculated |
|
79 using c=(2*r+5*g+b)/512. Note that the return value is rounded down to the |
|
80 nearest integer. */ |
|
81 { |
|
82 return Gray256() / 64; |
|
83 } |
|
84 |
|
85 int TRgb::Gray16() const |
|
86 /** Gets a 16 level grayscale value from this TRgb. |
|
87 |
|
88 @return The greyscale value is calculated using c=(2*r+5*g+b)/128. Note that |
|
89 the return value is rounded down to the nearest integer. */ |
|
90 { |
|
91 return Gray256() / 16; |
|
92 } |
|
93 |
|
94 int TRgb::Gray256() const |
|
95 /** Gets a 256 level grayscale value from this TRgb. |
|
96 |
|
97 @return The greyscale value is calculated using c=(2*r+5*g+b)/8. Note that |
|
98 the return value is rounded down to the nearest integer. */ |
|
99 { |
|
100 return((2*iRed+5*iGreen+iBlue)/8); |
|
101 } |
|
102 |
|
103 int TRgb::Color16() const |
|
104 /** Gets a 4 bit index into a colour palette from this TRgb. |
|
105 |
|
106 @return The EGA low colour constant closest to the TRgb. */ |
|
107 { |
|
108 int index = (iRed >> 5) & 0x007; |
|
109 index |= (iGreen >> 2) & 0x038; |
|
110 index |= (iBlue << 1) & 0x1c0; |
|
111 return color16inverse[index]; |
|
112 } |
|
113 |
|
114 int TRgb::Color256() const |
|
115 /** Gets an 8 bit index into a colour palette from this TRgb. |
|
116 |
|
117 @return An 8 bit index. */ |
|
118 { |
|
119 int index = (iRed >> 4) & 0x00f; |
|
120 index |= iGreen & 0x0f0; |
|
121 index |= (iBlue << 4) & 0xf00; |
|
122 |
|
123 if (color256InversePalette) |
|
124 return color256InversePalette[index]; |
|
125 else |
|
126 return color256inverse[index]; |
|
127 } |
|
128 |
|
129 int TRgb::Color4K() const |
|
130 /** Gets a 12 bit index into a colour palette from this TRgb. |
|
131 |
|
132 @return A 12 bit index. */ |
|
133 { |
|
134 return(((iRed&0xf0)<<4)|(iGreen&0xf0)|((iBlue&0xf0)>>4)); |
|
135 } |
|
136 |
|
137 int TRgb::Color64K() const |
|
138 /** Gets a 24 bit index into a colour palette from this TRgb. |
|
139 |
|
140 @return A 24 bit index. */ |
|
141 { |
|
142 return(((iRed&0xf8)<<8)|((iGreen&0xfc)<<3)|((iBlue&0xf8)>>3)); |
|
143 } |
|
144 |
|
145 long int TRgb::Color16M() const |
|
146 /** Gets a 16 bit index into a colour palette from this TRgb. |
|
147 |
|
148 @return A 16 bit index. */ |
|
149 { |
|
150 return((iRed<<16)|(iGreen<<8)|iBlue); |
|
151 } |
|
152 |
|
153 TRgb TRgb::Gray2(int aGray2) |
|
154 /** Gets TRgb from 2 level grayscale. |
|
155 |
|
156 The function takes a grayscale argument and return a TRgb whose red, green |
|
157 and blue values are set to an appropriate level. |
|
158 |
|
159 @param aGray2 Grayscale value to be converted. |
|
160 @return Equivalent 24 bit colour. Gray2 has only 2 levels (black and white), - |
|
161 the function returns r=g=b=0 or r=g=b=255. */ |
|
162 { |
|
163 aGray2 *= 255; |
|
164 return TRgb(aGray2,aGray2,aGray2); |
|
165 } |
|
166 |
|
167 TRgb TRgb::Gray4(int aGray4) |
|
168 /** Gets TRgb from 4 level grayscale. |
|
169 |
|
170 The function takes a grayscale argument and return a TRgb whose red, green |
|
171 and blue values are set to an appropriate level. |
|
172 |
|
173 @param aGray4 Grayscale value to be converted. |
|
174 @return Equivalent 24 bit colour. Gray4 has 4 levels- the function returns |
|
175 r=g=b=85*c, where c=0,1,2, or 3. */ |
|
176 { |
|
177 aGray4 *= 85; |
|
178 return TRgb(aGray4,aGray4,aGray4); |
|
179 } |
|
180 |
|
181 TRgb TRgb::Gray16(int aGray16) |
|
182 /** Gets TRgb from 16 level grayscale. |
|
183 |
|
184 The function takes a grayscale argument and return a TRgb whose red, green |
|
185 and blue values are set to an appropriate level. |
|
186 |
|
187 @param aGray16 Grayscale value to be converted. |
|
188 @return Equivalent 24 bit colour. Gray16 has 16 levels- the function returns |
|
189 r=g=b=17*c, where c=0, 1, ... 15. */ |
|
190 { |
|
191 aGray16 *= 17; |
|
192 return TRgb(aGray16,aGray16,aGray16); |
|
193 } |
|
194 |
|
195 TRgb TRgb::Gray256(int aGray256) |
|
196 /** Gets TRgb from 256 level grayscale. |
|
197 |
|
198 The function takes a grayscale argument and return a TRgb whose red, green |
|
199 and blue values are set to an appropriate level. |
|
200 |
|
201 @param aGray256 Grayscale value to be converted. |
|
202 @return Equivalent 24 bit colour. Gray256 has 256 levels- the function |
|
203 returns r=g=b=c, where c=0, 1, ... 255. */ |
|
204 { |
|
205 return TRgb(aGray256,aGray256,aGray256); |
|
206 } |
|
207 |
|
208 TRgb TRgb::Color16(int aColor16) |
|
209 /** Gets TRgb from 4 bit colour index. |
|
210 |
|
211 The function takes a 4 bit index into a colour palette and returns a TRgb |
|
212 whose red, green and blue values are set to an appropriate level. |
|
213 |
|
214 @param aColor16 4 bit index into a colour palette |
|
215 @return Equivalent 24 bit colour. */ |
|
216 { |
|
217 return TRgb(color16array[aColor16&0xf]); |
|
218 } |
|
219 |
|
220 TRgb TRgb::Color256(int aColor256) |
|
221 /** Gets TRgb from 8 bit colour index. |
|
222 |
|
223 The function takes an 8 bit index into a colour palette and returns a TRgb |
|
224 whose red, green and blue values are set to an appropriate level. |
|
225 |
|
226 @param aColor256 8 bit index into a colour palette. |
|
227 @return Equivalent 24 bit colour. */ |
|
228 { |
|
229 if (color256Palette) |
|
230 return color256Palette[aColor256&0xff]; |
|
231 else |
|
232 return TRgb(color256array[aColor256&0xff]); |
|
233 } |
|
234 |
|
235 TRgb TRgb::Color4K(int aColor4K) |
|
236 { |
|
237 return TRgb(((aColor4K>>8)&0xf)*17,((aColor4K>>4)&0xf)*17,(aColor4K&0xf)*17); |
|
238 } |
|
239 |
|
240 TRgb TRgb::Color64K(int aColor64K) |
|
241 /** Gets TRgb from 64K colour index. |
|
242 |
|
243 The function takes a 16 bit index into a colour palette and returns a TRgb |
|
244 whose red, green and blue values are set to an appropriate level. |
|
245 |
|
246 @param aColor64K 16 bit index into a colour palette |
|
247 @return Equivalent 24 bit colour. */ |
|
248 { |
|
249 return TRgb(((aColor64K>>11)&0x1f)*255/31,((aColor64K>>5)&0x3f)*255/63,(aColor64K&0x1f)*255/31); |
|
250 } |
|
251 |
|
252 TRgb TRgb::Color16M(long int aColor16M) |
|
253 /** Gets TRgb from 16M colour index. |
|
254 |
|
255 The function takes a 24 bit index into a colour palette and returns the TRgb |
|
256 whose red, green and blue values represent it exactly. |
|
257 |
|
258 @param aColor16M 24 bit index into a colour palette |
|
259 @return The TRgb which represents the index exactly. */ |
|
260 { |
|
261 return TRgb(((aColor16M>>16)&0xff),(aColor16M>>8)&0xff,aColor16M&0xff); |
|
262 } |
|
263 |