LibreOffice Module xmerge (master) 1
ColourConverter.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.util;
20
21import java.awt.Color;
22
41public class ColourConverter {
42
44 private static final short BLACK = 0;
45
47 private static final short SILVER = 1;
48
50 private static final short GREY = 2;
51
53 private static final short WHITE = 3;
54
56 private static final short RED = 4;
57
59 private static final short LIME = 5;
60
62 private static final short BLUE = 6;
63
65 private static final short AQUA = 7;
66
68 private static final short FUCHSIA = 8;
69
71 private static final short YELLOW = 9;
72
74 private static final short MAROON = 10;
75
77 private static final short GREEN = 11;
78
80 private static final short NAVY = 12;
81
83 private static final short TEAL = 13;
84
86 private static final short PURPLE = 14;
87
89 public static final short OLIVE = 15;
90
91 private short tableLookup[] = null;
92
96 public ColourConverter() {
97
98 }
99
108 public ColourConverter(short lookup[]) {
109 tableLookup = lookup;
110 }
111
116 private short colourLookup(short colour) {
117 if(tableLookup!=null) {
118 return tableLookup[colour];
119 } else {
120 return colour;
121 }
122 }
123
128 private short indexLookup(short index) {
129
130 short result = 0;
131
132 if(tableLookup!=null) {
133 for(short i = 0;i < tableLookup.length;i++) {
134 if(tableLookup[i]==index)
135 result = i;
136 }
137 } else {
138 result = index;
139 }
140
141 return result;
142 }
152 public Color convertToRGB (short colour) {
153
154 short index = indexLookup(colour);
155
156 int r = 0;
157 int g = 0;
158 int b = 0;
159
160 switch (index) {
161 case SILVER:
162 r = g = b = 128;
163 break;
164
165 case GREY:
166 r = g = b = 192;
167 break;
168
169 case WHITE:
170 r = g = b = 255;
171 break;
172
173 case RED:
174 r = 255;
175 break;
176
177 case LIME:
178 g = 255;
179 break;
180
181 case BLUE:
182 b = 255;
183 break;
184
185 case AQUA:
186 g = b = 255;
187 break;
188
189 case FUCHSIA:
190 r = b = 255;
191 break;
192
193 case YELLOW:
194 r = g = 255;
195 break;
196
197 case MAROON:
198 r = 128;
199 break;
200
201 case GREEN:
202 g = 128;
203 break;
204
205 case NAVY:
206 b = 128;
207 break;
208
209 case TEAL:
210 b = g = 128;
211 break;
212
213 case PURPLE:
214 r = b = 128;
215 break;
216
217 case OLIVE:
218 r = g = 128;
219 break;
220
221 case BLACK:
222 default:
223 r = g = b = 0;
224 break;
225 }
226
227 return new Color(r, g, b);
228 }
229
243 public short convertFromRGB (Color colour) {
244 int matchedRGB = 0;
245 short indexColour = 0;
246 int reducedMap[] = new int[] { 0, 0, 128 };
247
248 int red = colour.getRed();
249 int green = colour.getGreen();
250 int blue = colour.getBlue();
251
252 // We need to convert the pale colours to their base color rather than
253 // white so we modify the rgb values if the colour is sufficiently white.
254 if(red>0xC0 && green>0xC0 && blue>0xC0) {
255
256 if(red!=0xFF)
257 red = getClosest(red, reducedMap);
258 if(green!=0xFF)
259 green = getClosest(green, reducedMap);
260 if(blue!=0xFF)
261 blue = getClosest(blue, reducedMap);
262 }
263
264 // Need to derive an RGB value that has been rounded to match the ones
265 // Pocket Word knows about.
266 matchedRGB += getClosest(red) << 16;
267 matchedRGB += getClosest(green) << 8;
268 matchedRGB += getClosest(blue);
269
270 // The colour map used by Pocket Word doesn't have any combinations of
271 // values beyond 0 and any other value. A value of 255 in any RGB code
272 // indicates a dominant colour. Other colours are only modifiers to the
273 // principal colour(s). Thus, for this conversion, modifiers can be
274 // dropped.
275 if ((matchedRGB & 0xFF0000) == 0xFF0000 || (matchedRGB & 0xFF00) == 0xFF00
276 || (matchedRGB & 0xFF) == 0xFF) {
277 if ((matchedRGB & 0xFF0000) == 0x800000) {
278 matchedRGB ^= 0x800000;
279 }
280 if ((matchedRGB & 0xFF00) == 0x8000) {
281 matchedRGB ^= 0x8000;
282 }
283 if ((matchedRGB & 0xFF) == 0x80) {
284 matchedRGB ^= 0x80;
285 }
286 }
287
288 /*
289 * And now for the actual matching ...
290 *
291 * Colours are based on the Windows VGA 16 palette. One difference
292 * though is that Pocket Word seems to switch the RGB codes for Grey
293 * and Silver. In Pocket Word Silver is the darker colour leaving Grey
294 * is closest to White.
295 *
296 * Shades of grey will be converted to either Silver or White, where
297 * Grey may be a more appropriate colour. This is handled specially
298 * only for Silver and White matches.
299 */
300 switch (matchedRGB) {
301 case 0x000000:
302 indexColour = BLACK;
303 break;
304
305 case 0x808080:
306 if (!isGrey(colour)) {
307 indexColour = SILVER;
308 }
309 else {
310 indexColour = GREY;
311 }
312 break;
313
314 case 0xFFFFFF:
315 if (!isGrey(colour)) {
316 indexColour = WHITE;
317 }
318 else {
319 indexColour = GREY;
320 }
321 break;
322
323 case 0xFF0000:
324 indexColour = RED;
325 break;
326
327 case 0x00FF00:
328 indexColour = LIME;
329 break;
330
331 case 0x0000FF:
332 indexColour = BLUE;
333 break;
334
335 case 0x00FFFF:
336 indexColour = AQUA;
337 break;
338
339 case 0xFF00FF:
340 indexColour = FUCHSIA;
341 break;
342
343 case 0xFFFF00:
344 indexColour = YELLOW;
345 break;
346
347 case 0x800000:
348 indexColour = MAROON;
349 break;
350
351 case 0x008000:
352 indexColour = GREEN;
353 break;
354
355 case 0x000080:
356 indexColour = NAVY;
357 break;
358
359 case 0x008080:
360 indexColour = TEAL;
361 break;
362
363 case 0x800080:
364 indexColour = PURPLE;
365 break;
366
367 case 0x808000:
368 indexColour = OLIVE;
369 break;
370
371 default: // Just in case!
372 indexColour = BLACK;
373 break;
374 }
375
376 return colourLookup(indexColour);
377 }
378
382 private int getClosest(int value) {
383 int points[] = new int[] { 0, 128, 255 };
384
385 return getClosest(value, points);
386 }
387
392 private int getClosest(int value, int[] points) {
393
394 if (value == points[0] || value == points[1] || value == points[2]) {
395 return value;
396 }
397
398 if (value < points[1]) {
399 int x = value - points[0];
400 return (Math.round((float)x / (points[1] - points[0])) == 1 ? points[1] : points[0]);
401 }
402 else {
403 int x = value - points[1];
404 return (Math.round((float)x / (points[2] - points[1])) >= 1 ? points[2] : points[1]);
405 }
406 }
407
411 private boolean isGrey(Color c) {
412 int matchedRGB = 0;
413 int points[] = new int[] { 128, 192, 255 };
414
415 matchedRGB += getClosest(c.getRed(), points) << 16;
416 matchedRGB += getClosest(c.getGreen(), points) << 8;
417 matchedRGB += getClosest(c.getBlue(), points);
418
419 return matchedRGB == 0xC0C0C0;
420 }
421}
Utility class mapping RGB colour specifications to the colour indices used in the Pocket PC.
static final short WHITE
Colour table index for White.
ColourConverter()
Default constructor used in the case where a lookup table is not required.
static final short BLACK
Colour table index for Black.
int getClosest(int value, int[] points)
Utility method that returns the closest of the three points to the value supplied.
static final short GREEN
Colour table index for Green.
short convertFromRGB(Color colour)
This method approximates an RGB value (as used by Writer) to one of the 16 available colours.
static final short MAROON
Colour table index for Maroon.
static final short FUCHSIA
Colour table index for Fuchsia.
static final short SILVER
Colour table index for Silver.
Color convertToRGB(short colour)
This method maps a Pocket Word colour index value to an RGB value as used by OpenOffice.
static final short PURPLE
Colour table index for Purple.
static final short AQUA
Colour table index for Aqua.
static final short LIME
Colour table index for Lime.
ColourConverter(short lookup[])
Constructor that passes in the colour lookup table.
int getClosest(int value)
Default implementation, checks for the closest of value to 0, 128 or 255.
static final short BLUE
Colour table index for Blue.
static final short RED
Colour table index for Red.
static final short OLIVE
Colour table index for Olive.
static final short YELLOW
Colour table index for Yellow.
static final short GREY
Colour table index for Grey.
static final short NAVY
Colour table index for Navy.
short indexLookup(short index)
Uses the colour table if it exists to translate default values to values in the colorTable.
boolean isGrey(Color c)
Checks to see if the supplied colour can be considered to be grey.
static final short TEAL
Colour table index for Teal.
short colourLookup(short colour)
Uses the colour table if it exists to translate default values to values in the colorTable.
Any value
float x
int i
index
Any result