Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
KodeMunkie committed Sep 30, 2023
1 parent 0e6189f commit 5fbfe4b
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions src/main/java/uk/co/silentsoftware/core/helpers/ColourHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.time.Duration;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;

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

private static final int MAXIMUM_COMPONENT_VALUE = 255;

private static final int PREFER_DETAIL_COMPONENT_BOUNDARY = 127;

private static final int CACHE_TIME_SECONDS = 10;

private static final Cache<String, GigaScreenAttribute.GigaScreenColour> CACHE = Caffeine.newBuilder().expireAfterAccess(CACHE_TIME_SECONDS, TimeUnit.SECONDS).build();
private static final Cache<String, GigaScreenAttribute.GigaScreenColour> CACHE = Caffeine.newBuilder().expireAfterAccess(Duration.ofSeconds(CACHE_TIME_SECONDS)).build();

private static final Cache<String, int[]> AVERAGE_CACHE = Caffeine.newBuilder().expireAfterAccess(CACHE_TIME_SECONDS, TimeUnit.SECONDS).build();
private static final Cache<String, int[]> AVERAGE_CACHE = Caffeine.newBuilder().expireAfterAccess(Duration.ofSeconds(CACHE_TIME_SECONDS)).build();


/**
Expand Down Expand Up @@ -205,20 +203,20 @@ public static int[] getAverageColourDistance(int[] palette) {
* selecting xMax by yMax parts of the output image (i.e. usually 8x8
* pixels), chooses the most popular two colours. The colour choice strategy
* then decides how to colour individual pixels based on these two colours.
*
*
* Note it is expected that this method will be called AFTER the pixels have
* been changed to Spectrum colours.
*
*
* @param image the image to colour
* @param colourChoiceStrategy the colour choice strategy
* @return the modified image
*/
public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceStrategy colourChoiceStrategy) {

// Do not use bidimap because inverse map key values will be lost (i.e. many tallies will produce same key)
// Do not use bidimap because inverse map key values will be lost (i.e. many tallies will produce same key)
Map<Integer, Integer> map = new HashMap<>();
List<TallyValue> tallyValues = new LinkedList<>();

// Analyse block and choose the two most popular colours in attribute block
for (int y = 0; y + ATTRIBUTE_BLOCK_SIZE <= image.getHeight(); y += ATTRIBUTE_BLOCK_SIZE) {
for (int x = 0; x + ATTRIBUTE_BLOCK_SIZE <= image.getWidth() && y + ATTRIBUTE_BLOCK_SIZE <= image.getHeight(); x += ATTRIBUTE_BLOCK_SIZE) {
Expand All @@ -240,15 +238,15 @@ public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceSt
tallyValues.add(new TallyValue(colour, tally));
});
tallyValues.sort(TallyValue.TALLY_COMPARATOR);

int mostPopularColour = tallyValues.get(0).getColour();
int secondMostPopularColour = tallyValues.size()>1?tallyValues.get(1).getColour():mostPopularColour;

// Enforce attribute favouritism rules on the two spectrum
// attribute colours (fixes the problem that colours could be from both the bright
// and half bright set).
int[] correctedAlphaColours = OptionsObject.getInstance().getAttributeMode().enforceAttributeRule(mostPopularColour, secondMostPopularColour);

// Replace all colours in attribute block (which can be any spectrum colours) with the just the popular two
for (int i = 0; i < outRgb.length; ++i) {
outRgb[i] = colourChoiceStrategy.chooseBestPaletteMatch(outRgb[i], correctedAlphaColours);
Expand All @@ -262,7 +260,7 @@ public static BufferedImage colourAttributes(BufferedImage image, ColourChoiceSt
/**
* Determines whether the colour is from the Spectrum's bright or half
* bright colour set.
*
*
* @param rgb the colour to test
* @return whether this colour is in the bright set
*/
Expand All @@ -281,9 +279,9 @@ public static boolean isBrightSet(int rgb) {

/**
* Changes the contrast of an image
*
*
* @see java.awt.image.RescaleOp
*
*
* @param img the image to change the contrast of
* @param amount the amount to change it (scale factor)
* @return the modified image
Expand All @@ -298,9 +296,9 @@ public static BufferedImage changeContrast(BufferedImage img, float amount) {

/**
* Changes brightness by increasing all pixel values by a given amount
*
*
* @see java.awt.image.RescaleOp
*
*
* @param img the image to change the brightness of
* @param amount the amount to change it
* @return the modified image
Expand All @@ -315,7 +313,7 @@ public static BufferedImage changeBrightness(BufferedImage img, float amount) {

/**
* Changes image saturation by a given amount
*
*
* @param img the image to change
* @param amount the amount of saturation (0-1 range)
* @return the modified image
Expand All @@ -335,7 +333,7 @@ public static BufferedImage changeSaturation(BufferedImage img, float amount) {
/**
* Changes the saturation of an individual pixel by the given amount (0-1
* range)
*
*
* @param pixel the pixel rgb to saturate
* @param amount the amount to saturate
* @return the modified rgb pixel
Expand All @@ -351,7 +349,7 @@ private static int changePixelSaturation(int pixel, float amount) {
/**
* Ensures a value is within a given range. If it exceeds or is below it is
* set to the high value or low value respectively
*
*
* @param value the value to test
* @param low the low value
* @param high the high value
Expand All @@ -366,11 +364,11 @@ private static int correctRange(int value, int low, int high) {
}
return value;
}

/**
* Ensures a value is within a given range. If it exceeds or is below it is
* set to the high value or low value respectively
*
*
* @param value the value to test
* @param low the low value
* @param high the high value
Expand All @@ -388,7 +386,7 @@ public static float correctRange(float value, float low, float high) {

/**
* Convert rgb to its components
*
*
* @param rgb the value to split
* @return the rgb components
*/
Expand All @@ -398,7 +396,7 @@ public static int[] intToRgbComponents(int rgb) {

/**
* Convert individual RGB components into a 32 bit ARGB value
*
*
* @param red the red component
* @param green the green component
* @param blue the blue component
Expand All @@ -410,7 +408,7 @@ public static int componentsToAlphaRgb(int red, int green, int blue) {

/**
* Corrects and individual colour component value's range to 0>channel<255
*
*
* @param component the component to restrict
* @return the corrected component
*/
Expand All @@ -421,9 +419,9 @@ public static int correctRange(int component) {
/**
* Returns an array of black and white colours representing the ink (black)
* and paper (white) monochrome colours.
*
*
* Opposite function to getMonochromeFromBlackAndWhite
*
*
* @param image the monochrome image to scan
* @return an array of black and white rgb values
*/
Expand All @@ -442,9 +440,9 @@ public static int[] getBlackAndWhiteFromMonochrome(int[] image) {
/**
* Returns an array of monochrome (chosen ink and paper) colours based on an
* input array of black (ink) and white (paper).
*
*
* Opposite function to getBlackAndWhiteFromMonochrome
*
*
* @param data the black and white array to get the monochrome colours for
* @return the equivalent monochrome array
*/
Expand All @@ -459,7 +457,7 @@ public static int[] getMonochromeFromBlackAndWhite(final int[] data) {
/**
* Returns an a monochrome (chosen ink and paper) colour based on an
* input array of black (ink) and white (paper).
*
*
* @param original the black and white rgb value to get the monochrome colour for
* @return the equivalent monochrome colour
*/
Expand Down

0 comments on commit 5fbfe4b

Please sign in to comment.