Skip to content

Commit 5fbfe4b

Browse files
committed
Code cleanup
1 parent 0e6189f commit 5fbfe4b

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/main/java/uk/co/silentsoftware/core/helpers/ColourHelper.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import java.awt.*;
3030
import java.awt.image.BufferedImage;
3131
import java.awt.image.RescaleOp;
32+
import java.time.Duration;
3233
import java.util.List;
3334
import java.util.*;
34-
import java.util.concurrent.TimeUnit;
3535

3636
import static uk.co.silentsoftware.config.SpectrumDefaults.ATTRIBUTE_BLOCK_SIZE;
3737
/**
@@ -41,13 +41,11 @@ public final class ColourHelper {
4141

4242
private static final int MAXIMUM_COMPONENT_VALUE = 255;
4343

44-
private static final int PREFER_DETAIL_COMPONENT_BOUNDARY = 127;
45-
4644
private static final int CACHE_TIME_SECONDS = 10;
4745

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();
4947

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();
5149

5250

5351
/**
@@ -205,20 +203,20 @@ public static int[] getAverageColourDistance(int[] palette) {
205203
* selecting xMax by yMax parts of the output image (i.e. usually 8x8
206204
* pixels), chooses the most popular two colours. The colour choice strategy
207205
* then decides how to colour individual pixels based on these two colours.
208-
*
206+
*
209207
* Note it is expected that this method will be called AFTER the pixels have
210208
* been changed to Spectrum colours.
211-
*
209+
*
212210
* @param image the image to colour
213211
* @param colourChoiceStrategy the colour choice strategy
214212
* @return the modified image
215213
*/
216214
public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceStrategy colourChoiceStrategy) {
217215

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)
219217
Map<Integer, Integer> map = new HashMap<>();
220218
List<TallyValue> tallyValues = new LinkedList<>();
221-
219+
222220
// Analyse block and choose the two most popular colours in attribute block
223221
for (int y = 0; y + ATTRIBUTE_BLOCK_SIZE <= image.getHeight(); y += ATTRIBUTE_BLOCK_SIZE) {
224222
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
240238
tallyValues.add(new TallyValue(colour, tally));
241239
});
242240
tallyValues.sort(TallyValue.TALLY_COMPARATOR);
243-
241+
244242
int mostPopularColour = tallyValues.get(0).getColour();
245243
int secondMostPopularColour = tallyValues.size()>1?tallyValues.get(1).getColour():mostPopularColour;
246-
244+
247245
// Enforce attribute favouritism rules on the two spectrum
248246
// attribute colours (fixes the problem that colours could be from both the bright
249247
// and half bright set).
250248
int[] correctedAlphaColours = OptionsObject.getInstance().getAttributeMode().enforceAttributeRule(mostPopularColour, secondMostPopularColour);
251-
249+
252250
// Replace all colours in attribute block (which can be any spectrum colours) with the just the popular two
253251
for (int i = 0; i < outRgb.length; ++i) {
254252
outRgb[i] = colourChoiceStrategy.chooseBestPaletteMatch(outRgb[i], correctedAlphaColours);
@@ -262,7 +260,7 @@ public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceSt
262260
/**
263261
* Determines whether the colour is from the Spectrum's bright or half
264262
* bright colour set.
265-
*
263+
*
266264
* @param rgb the colour to test
267265
* @return whether this colour is in the bright set
268266
*/
@@ -281,9 +279,9 @@ public static boolean isBrightSet(int rgb) {
281279

282280
/**
283281
* Changes the contrast of an image
284-
*
282+
*
285283
* @see java.awt.image.RescaleOp
286-
*
284+
*
287285
* @param img the image to change the contrast of
288286
* @param amount the amount to change it (scale factor)
289287
* @return the modified image
@@ -298,9 +296,9 @@ public static BufferedImage changeContrast(BufferedImage img, float amount) {
298296

299297
/**
300298
* Changes brightness by increasing all pixel values by a given amount
301-
*
299+
*
302300
* @see java.awt.image.RescaleOp
303-
*
301+
*
304302
* @param img the image to change the brightness of
305303
* @param amount the amount to change it
306304
* @return the modified image
@@ -315,7 +313,7 @@ public static BufferedImage changeBrightness(BufferedImage img, float amount) {
315313

316314
/**
317315
* Changes image saturation by a given amount
318-
*
316+
*
319317
* @param img the image to change
320318
* @param amount the amount of saturation (0-1 range)
321319
* @return the modified image
@@ -335,7 +333,7 @@ public static BufferedImage changeSaturation(BufferedImage img, float amount) {
335333
/**
336334
* Changes the saturation of an individual pixel by the given amount (0-1
337335
* range)
338-
*
336+
*
339337
* @param pixel the pixel rgb to saturate
340338
* @param amount the amount to saturate
341339
* @return the modified rgb pixel
@@ -351,7 +349,7 @@ private static int changePixelSaturation(int pixel, float amount) {
351349
/**
352350
* Ensures a value is within a given range. If it exceeds or is below it is
353351
* set to the high value or low value respectively
354-
*
352+
*
355353
* @param value the value to test
356354
* @param low the low value
357355
* @param high the high value
@@ -366,11 +364,11 @@ private static int correctRange(int value, int low, int high) {
366364
}
367365
return value;
368366
}
369-
367+
370368
/**
371369
* Ensures a value is within a given range. If it exceeds or is below it is
372370
* set to the high value or low value respectively
373-
*
371+
*
374372
* @param value the value to test
375373
* @param low the low value
376374
* @param high the high value
@@ -388,7 +386,7 @@ public static float correctRange(float value, float low, float high) {
388386

389387
/**
390388
* Convert rgb to its components
391-
*
389+
*
392390
* @param rgb the value to split
393391
* @return the rgb components
394392
*/
@@ -398,7 +396,7 @@ public static int[] intToRgbComponents(int rgb) {
398396

399397
/**
400398
* Convert individual RGB components into a 32 bit ARGB value
401-
*
399+
*
402400
* @param red the red component
403401
* @param green the green component
404402
* @param blue the blue component
@@ -410,7 +408,7 @@ public static int componentsToAlphaRgb(int red, int green, int blue) {
410408

411409
/**
412410
* Corrects and individual colour component value's range to 0>channel<255
413-
*
411+
*
414412
* @param component the component to restrict
415413
* @return the corrected component
416414
*/
@@ -421,9 +419,9 @@ public static int correctRange(int component) {
421419
/**
422420
* Returns an array of black and white colours representing the ink (black)
423421
* and paper (white) monochrome colours.
424-
*
422+
*
425423
* Opposite function to getMonochromeFromBlackAndWhite
426-
*
424+
*
427425
* @param image the monochrome image to scan
428426
* @return an array of black and white rgb values
429427
*/
@@ -442,9 +440,9 @@ public static int[] getBlackAndWhiteFromMonochrome(int[] image) {
442440
/**
443441
* Returns an array of monochrome (chosen ink and paper) colours based on an
444442
* input array of black (ink) and white (paper).
445-
*
443+
*
446444
* Opposite function to getBlackAndWhiteFromMonochrome
447-
*
445+
*
448446
* @param data the black and white array to get the monochrome colours for
449447
* @return the equivalent monochrome array
450448
*/
@@ -459,7 +457,7 @@ public static int[] getMonochromeFromBlackAndWhite(final int[] data) {
459457
/**
460458
* Returns an a monochrome (chosen ink and paper) colour based on an
461459
* input array of black (ink) and white (paper).
462-
*
460+
*
463461
* @param original the black and white rgb value to get the monochrome colour for
464462
* @return the equivalent monochrome colour
465463
*/

0 commit comments

Comments
 (0)