29
29
import java .awt .*;
30
30
import java .awt .image .BufferedImage ;
31
31
import java .awt .image .RescaleOp ;
32
+ import java .time .Duration ;
32
33
import java .util .List ;
33
34
import java .util .*;
34
- import java .util .concurrent .TimeUnit ;
35
35
36
36
import static uk .co .silentsoftware .config .SpectrumDefaults .ATTRIBUTE_BLOCK_SIZE ;
37
37
/**
@@ -41,13 +41,11 @@ public final class ColourHelper {
41
41
42
42
private static final int MAXIMUM_COMPONENT_VALUE = 255 ;
43
43
44
- private static final int PREFER_DETAIL_COMPONENT_BOUNDARY = 127 ;
45
-
46
44
private static final int CACHE_TIME_SECONDS = 10 ;
47
45
48
- private static final Cache <String , GigaScreenAttribute .GigaScreenColour > CACHE = Caffeine .newBuilder ().expireAfterAccess (CACHE_TIME_SECONDS , TimeUnit . SECONDS ).build ();
46
+ private static final Cache <String , GigaScreenAttribute .GigaScreenColour > CACHE = Caffeine .newBuilder ().expireAfterAccess (Duration . ofSeconds ( CACHE_TIME_SECONDS ) ).build ();
49
47
50
- private static final Cache <String , int []> AVERAGE_CACHE = Caffeine .newBuilder ().expireAfterAccess (CACHE_TIME_SECONDS , TimeUnit . SECONDS ).build ();
48
+ private static final Cache <String , int []> AVERAGE_CACHE = Caffeine .newBuilder ().expireAfterAccess (Duration . ofSeconds ( CACHE_TIME_SECONDS ) ).build ();
51
49
52
50
53
51
/**
@@ -205,20 +203,20 @@ public static int[] getAverageColourDistance(int[] palette) {
205
203
* selecting xMax by yMax parts of the output image (i.e. usually 8x8
206
204
* pixels), chooses the most popular two colours. The colour choice strategy
207
205
* then decides how to colour individual pixels based on these two colours.
208
- *
206
+ *
209
207
* Note it is expected that this method will be called AFTER the pixels have
210
208
* been changed to Spectrum colours.
211
- *
209
+ *
212
210
* @param image the image to colour
213
211
* @param colourChoiceStrategy the colour choice strategy
214
212
* @return the modified image
215
213
*/
216
214
public static BufferedImage colourAttributes (BufferedImage image , ColourChoiceStrategy colourChoiceStrategy ) {
217
215
218
- // Do not use bidimap because inverse map key values will be lost (i.e. many tallies will produce same key)
216
+ // Do not use bidimap because inverse map key values will be lost (i.e. many tallies will produce same key)
219
217
Map <Integer , Integer > map = new HashMap <>();
220
218
List <TallyValue > tallyValues = new LinkedList <>();
221
-
219
+
222
220
// Analyse block and choose the two most popular colours in attribute block
223
221
for (int y = 0 ; y + ATTRIBUTE_BLOCK_SIZE <= image .getHeight (); y += ATTRIBUTE_BLOCK_SIZE ) {
224
222
for (int x = 0 ; x + ATTRIBUTE_BLOCK_SIZE <= image .getWidth () && y + ATTRIBUTE_BLOCK_SIZE <= image .getHeight (); x += ATTRIBUTE_BLOCK_SIZE ) {
@@ -240,15 +238,15 @@ public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceSt
240
238
tallyValues .add (new TallyValue (colour , tally ));
241
239
});
242
240
tallyValues .sort (TallyValue .TALLY_COMPARATOR );
243
-
241
+
244
242
int mostPopularColour = tallyValues .get (0 ).getColour ();
245
243
int secondMostPopularColour = tallyValues .size ()>1 ?tallyValues .get (1 ).getColour ():mostPopularColour ;
246
-
244
+
247
245
// Enforce attribute favouritism rules on the two spectrum
248
246
// attribute colours (fixes the problem that colours could be from both the bright
249
247
// and half bright set).
250
248
int [] correctedAlphaColours = OptionsObject .getInstance ().getAttributeMode ().enforceAttributeRule (mostPopularColour , secondMostPopularColour );
251
-
249
+
252
250
// Replace all colours in attribute block (which can be any spectrum colours) with the just the popular two
253
251
for (int i = 0 ; i < outRgb .length ; ++i ) {
254
252
outRgb [i ] = colourChoiceStrategy .chooseBestPaletteMatch (outRgb [i ], correctedAlphaColours );
@@ -262,7 +260,7 @@ public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceSt
262
260
/**
263
261
* Determines whether the colour is from the Spectrum's bright or half
264
262
* bright colour set.
265
- *
263
+ *
266
264
* @param rgb the colour to test
267
265
* @return whether this colour is in the bright set
268
266
*/
@@ -281,9 +279,9 @@ public static boolean isBrightSet(int rgb) {
281
279
282
280
/**
283
281
* Changes the contrast of an image
284
- *
282
+ *
285
283
* @see java.awt.image.RescaleOp
286
- *
284
+ *
287
285
* @param img the image to change the contrast of
288
286
* @param amount the amount to change it (scale factor)
289
287
* @return the modified image
@@ -298,9 +296,9 @@ public static BufferedImage changeContrast(BufferedImage img, float amount) {
298
296
299
297
/**
300
298
* Changes brightness by increasing all pixel values by a given amount
301
- *
299
+ *
302
300
* @see java.awt.image.RescaleOp
303
- *
301
+ *
304
302
* @param img the image to change the brightness of
305
303
* @param amount the amount to change it
306
304
* @return the modified image
@@ -315,7 +313,7 @@ public static BufferedImage changeBrightness(BufferedImage img, float amount) {
315
313
316
314
/**
317
315
* Changes image saturation by a given amount
318
- *
316
+ *
319
317
* @param img the image to change
320
318
* @param amount the amount of saturation (0-1 range)
321
319
* @return the modified image
@@ -335,7 +333,7 @@ public static BufferedImage changeSaturation(BufferedImage img, float amount) {
335
333
/**
336
334
* Changes the saturation of an individual pixel by the given amount (0-1
337
335
* range)
338
- *
336
+ *
339
337
* @param pixel the pixel rgb to saturate
340
338
* @param amount the amount to saturate
341
339
* @return the modified rgb pixel
@@ -351,7 +349,7 @@ private static int changePixelSaturation(int pixel, float amount) {
351
349
/**
352
350
* Ensures a value is within a given range. If it exceeds or is below it is
353
351
* set to the high value or low value respectively
354
- *
352
+ *
355
353
* @param value the value to test
356
354
* @param low the low value
357
355
* @param high the high value
@@ -366,11 +364,11 @@ private static int correctRange(int value, int low, int high) {
366
364
}
367
365
return value ;
368
366
}
369
-
367
+
370
368
/**
371
369
* Ensures a value is within a given range. If it exceeds or is below it is
372
370
* set to the high value or low value respectively
373
- *
371
+ *
374
372
* @param value the value to test
375
373
* @param low the low value
376
374
* @param high the high value
@@ -388,7 +386,7 @@ public static float correctRange(float value, float low, float high) {
388
386
389
387
/**
390
388
* Convert rgb to its components
391
- *
389
+ *
392
390
* @param rgb the value to split
393
391
* @return the rgb components
394
392
*/
@@ -398,7 +396,7 @@ public static int[] intToRgbComponents(int rgb) {
398
396
399
397
/**
400
398
* Convert individual RGB components into a 32 bit ARGB value
401
- *
399
+ *
402
400
* @param red the red component
403
401
* @param green the green component
404
402
* @param blue the blue component
@@ -410,7 +408,7 @@ public static int componentsToAlphaRgb(int red, int green, int blue) {
410
408
411
409
/**
412
410
* Corrects and individual colour component value's range to 0>channel<255
413
- *
411
+ *
414
412
* @param component the component to restrict
415
413
* @return the corrected component
416
414
*/
@@ -421,9 +419,9 @@ public static int correctRange(int component) {
421
419
/**
422
420
* Returns an array of black and white colours representing the ink (black)
423
421
* and paper (white) monochrome colours.
424
- *
422
+ *
425
423
* Opposite function to getMonochromeFromBlackAndWhite
426
- *
424
+ *
427
425
* @param image the monochrome image to scan
428
426
* @return an array of black and white rgb values
429
427
*/
@@ -442,9 +440,9 @@ public static int[] getBlackAndWhiteFromMonochrome(int[] image) {
442
440
/**
443
441
* Returns an array of monochrome (chosen ink and paper) colours based on an
444
442
* input array of black (ink) and white (paper).
445
- *
443
+ *
446
444
* Opposite function to getBlackAndWhiteFromMonochrome
447
- *
445
+ *
448
446
* @param data the black and white array to get the monochrome colours for
449
447
* @return the equivalent monochrome array
450
448
*/
@@ -459,7 +457,7 @@ public static int[] getMonochromeFromBlackAndWhite(final int[] data) {
459
457
/**
460
458
* Returns an a monochrome (chosen ink and paper) colour based on an
461
459
* input array of black (ink) and white (paper).
462
- *
460
+ *
463
461
* @param original the black and white rgb value to get the monochrome colour for
464
462
* @return the equivalent monochrome colour
465
463
*/
0 commit comments