Code cleanup (#50)

Minor code refactoring and formatting.
This commit is contained in:
CodeDead 2023-05-01 19:49:32 +02:00 committed by GitHub
parent a949420f6a
commit 7ebfa7ac0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 47 deletions

@ -8,12 +8,12 @@ AtlantaFX is based on GitHub Primer color system. You can check [GitHub Primer i
### Foreground Colors ### Foreground Colors
| Color |Usage | | Color | Usage |
|----------------------|--------------------------------| |----------------------|----------------------------------------------------------------------------------------------------------------------------------|
| `-color-fg-default` | Primary color for text and icons. It should be used for body content, titles and labels. | | `-color-fg-default` | Primary color for text and icons. It should be used for body content, titles and labels. |
| `-color-fg-muted` | For content that is secondary or that provides additional context but is not critical to understanding the flow of an interface. | | `-color-fg-muted` | For content that is secondary or that provides additional context but is not critical to understanding the flow of an interface. |
| `-color-fg-subtle` | For placeholders or decorative foregrounds. | | `-color-fg-subtle` | For placeholders or decorative foregrounds. |
| `-color-fg-emphasis` | The text color designed to combine with `*-emphasis` backgrounds for optimal contrast. | | `-color-fg-emphasis` | The text color designed to combine with `*-emphasis` backgrounds for optimal contrast. |
### Background Colors ### Background Colors

@ -14,6 +14,8 @@ import atlantafx.sampler.page.SampleBlock;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Cursor; import javafx.scene.Cursor;
import javafx.scene.control.Label; import javafx.scene.control.Label;
@ -127,9 +129,10 @@ public class CustomTextFieldPage extends AbstractPage {
timeField.textProperty().addListener((obs, old, val) -> { timeField.textProperty().addListener((obs, old, val) -> {
if (val != null) { if (val != null) {
try { try {
//noinspection ResultOfMethodCallIgnored
LocalTime.parse(val, timeFormatter); LocalTime.parse(val, timeFormatter);
timeField.pseudoClassStateChanged(STATE_DANGER, false); timeField.pseudoClassStateChanged(STATE_DANGER, false);
} catch (Exception e) { } catch (DateTimeParseException e) {
timeField.pseudoClassStateChanged(STATE_DANGER, true); timeField.pseudoClassStateChanged(STATE_DANGER, true);
} }
} }

@ -24,22 +24,28 @@ import java.util.List;
@SuppressWarnings({"unused", "NarrowingCompoundAssignment"}) @SuppressWarnings({"unused", "NarrowingCompoundAssignment"})
final class ColorThief { final class ColorThief {
private ColorThief() {
// Default constructor
}
private static final int DEFAULT_QUALITY = 10; private static final int DEFAULT_QUALITY = 10;
private static final boolean DEFAULT_IGNORE_WHITE = true; private static final boolean DEFAULT_IGNORE_WHITE = true;
public static int[] getColor(BufferedImage source) { public static int[] getColor(BufferedImage source) {
int[][] palette = getPalette(source, 5); int[][] palette = getPalette(source, 5);
if (palette == null) {
if (palette == null)
return null; return null;
}
return palette[0]; return palette[0];
} }
public static int[][] getPalette(BufferedImage source, int colorCount) { public static int[][] getPalette(BufferedImage source, int colorCount) {
MMCQ.ColorMap colorMap = getColorMap(source, colorCount); MMCQ.ColorMap colorMap = getColorMap(source, colorCount);
if (colorMap == null) {
if (colorMap == null)
return null; return null;
}
return colorMap.palette(); return colorMap.palette();
} }
@ -49,12 +55,10 @@ final class ColorThief {
public static MMCQ.ColorMap getColorMap(BufferedImage sourceImage, int colorCount, int quality, public static MMCQ.ColorMap getColorMap(BufferedImage sourceImage, int colorCount, int quality,
boolean ignoreWhite) { boolean ignoreWhite) {
if (colorCount < 2 || colorCount > 256) { if (colorCount < 2 || colorCount > 256)
throw new IllegalArgumentException("Specified colorCount must be between 2 and 256."); throw new IllegalArgumentException("Specified colorCount must be between 2 and 256.");
} if (quality < 1)
if (quality < 1) {
throw new IllegalArgumentException("Specified quality should be greater then 0."); throw new IllegalArgumentException("Specified quality should be greater then 0.");
}
int[][] pixelArray = switch (sourceImage.getType()) { int[][] pixelArray = switch (sourceImage.getType()) {
case TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR -> getPixelsFast(sourceImage, quality, ignoreWhite); case TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR -> getPixelsFast(sourceImage, quality, ignoreWhite);
@ -65,9 +69,9 @@ final class ColorThief {
} }
private static int[][] getPixelsFast( private static int[][] getPixelsFast(
BufferedImage sourceImage, BufferedImage sourceImage,
int quality, int quality,
boolean ignoreWhite) { boolean ignoreWhite) {
DataBufferByte imageData = (DataBufferByte) sourceImage.getRaster().getDataBuffer(); DataBufferByte imageData = (DataBufferByte) sourceImage.getRaster().getDataBuffer();
byte[] pixels = imageData.getData(); byte[] pixels = imageData.getData();
int pixelCount = sourceImage.getWidth() * sourceImage.getHeight(); int pixelCount = sourceImage.getWidth() * sourceImage.getHeight();
@ -83,7 +87,7 @@ final class ColorThief {
int expectedDataLength = pixelCount * colorDepth; int expectedDataLength = pixelCount * colorDepth;
if (expectedDataLength != pixels.length) { if (expectedDataLength != pixels.length) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"(expectedDataLength = " + expectedDataLength + ") != (pixels.length = " + pixels.length + ")" "(expectedDataLength = " + expectedDataLength + ") != (pixels.length = " + pixels.length + ")"
); );
} }
@ -94,7 +98,7 @@ final class ColorThief {
int offset, r, g, b, a; int offset, r, g, b, a;
switch (type) { switch (type) {
case TYPE_3BYTE_BGR: case TYPE_3BYTE_BGR -> {
for (int i = 0; i < pixelCount; i += quality) { for (int i = 0; i < pixelCount; i += quality) {
offset = i * 3; offset = i * 3;
b = pixels[offset] & 0xFF; b = pixels[offset] & 0xFF;
@ -102,13 +106,12 @@ final class ColorThief {
r = pixels[offset + 2] & 0xFF; r = pixels[offset + 2] & 0xFF;
if (!(ignoreWhite && r > 250 && g > 250 && b > 250)) { if (!(ignoreWhite && r > 250 && g > 250 && b > 250)) {
pixelArray[numUsedPixels] = new int[] {r, g, b}; pixelArray[numUsedPixels] = new int[]{r, g, b};
numUsedPixels++; numUsedPixels++;
} }
} }
break; }
case TYPE_4BYTE_ABGR -> {
case TYPE_4BYTE_ABGR:
for (int i = 0; i < pixelCount; i += quality) { for (int i = 0; i < pixelCount; i += quality) {
offset = i * 4; offset = i * 4;
a = pixels[offset] & 0xFF; a = pixels[offset] & 0xFF;
@ -117,23 +120,21 @@ final class ColorThief {
r = pixels[offset + 3] & 0xFF; r = pixels[offset + 3] & 0xFF;
if (a >= 125 && !(ignoreWhite && r > 250 && g > 250 && b > 250)) { if (a >= 125 && !(ignoreWhite && r > 250 && g > 250 && b > 250)) {
pixelArray[numUsedPixels] = new int[] {r, g, b}; pixelArray[numUsedPixels] = new int[]{r, g, b};
numUsedPixels++; numUsedPixels++;
} }
} }
break; }
default -> throw new IllegalArgumentException("Unhandled type: " + type);
default:
throw new IllegalArgumentException("Unhandled type: " + type);
} }
return Arrays.copyOfRange(pixelArray, 0, numUsedPixels); return Arrays.copyOfRange(pixelArray, 0, numUsedPixels);
} }
private static int[][] getPixelsSlow( private static int[][] getPixelsSlow(
BufferedImage sourceImage, BufferedImage sourceImage,
int quality, int quality,
boolean ignoreWhite) { boolean ignoreWhite) {
int width = sourceImage.getWidth(); int width = sourceImage.getWidth();
int height = sourceImage.getHeight(); int height = sourceImage.getHeight();
@ -153,7 +154,7 @@ final class ColorThief {
g = (rgb >> 8) & 0xFF; g = (rgb >> 8) & 0xFF;
b = rgb & 0xFF; b = rgb & 0xFF;
if (!(ignoreWhite && r > 250 && g > 250 && b > 250)) { if (!(ignoreWhite && r > 250 && g > 250 && b > 250)) {
res[numUsedPixels] = new int[] {r, g, b}; res[numUsedPixels] = new int[]{r, g, b};
numUsedPixels++; numUsedPixels++;
} }
} }
@ -259,10 +260,10 @@ final class ColorThief {
} }
if (ntot > 0) { if (ntot > 0) {
gAvg = new int[] {(rsum / ntot), (gsum / ntot), (bsum / ntot)}; gAvg = new int[]{(rsum / ntot), (gsum / ntot), (bsum / ntot)};
} else { } else {
gAvg = new int[] {(MULT * (r1 + r2 + 1) / 2), (MULT * (g1 + g2 + 1) / 2), gAvg = new int[]{(MULT * (r1 + r2 + 1) / 2), (MULT * (g1 + g2 + 1) / 2),
(MULT * (b1 + b2 + 1) / 2)}; (MULT * (b1 + b2 + 1) / 2)};
} }
} }
@ -279,7 +280,7 @@ final class ColorThief {
public static class ColorMap { public static class ColorMap {
public final ArrayList<VBox> vboxes = new ArrayList<>(); public final List<VBox> vboxes = new ArrayList<>();
public void push(VBox box) { public void push(VBox box) {
vboxes.add(box); vboxes.add(box);
@ -317,8 +318,8 @@ final class ColorThief {
for (VBox vbox : vboxes) { for (VBox vbox : vboxes) {
int[] vbColor = vbox.avg(false); int[] vbColor = vbox.avg(false);
d2 = Math.sqrt(Math.pow(color[0] - vbColor[0], 2) d2 = Math.sqrt(Math.pow(color[0] - vbColor[0], 2)
+ Math.pow(color[1] - vbColor[1], 2) + Math.pow(color[1] - vbColor[1], 2)
+ Math.pow(color[2] - vbColor[2], 2) + Math.pow(color[2] - vbColor[2], 2)
); );
if (d2 < d1) { if (d2 < d1) {
d1 = d2; d1 = d2;
@ -386,7 +387,7 @@ final class ColorThief {
} }
if (vbox.count(false) == 1) { if (vbox.count(false) == 1) {
return new VBox[] {vbox.clone(), null}; return new VBox[]{vbox.clone(), null};
} }
int rw = vbox.r2 - vbox.r1 + 1; int rw = vbox.r2 - vbox.r1 + 1;
@ -446,16 +447,16 @@ final class ColorThief {
} }
return maxw == rw ? doCut('r', vbox, partialSum, lookAheadSum, total) return maxw == rw ? doCut('r', vbox, partialSum, lookAheadSum, total)
: maxw == gw ? doCut('g', vbox, partialSum, lookAheadSum, total) : maxw == gw ? doCut('g', vbox, partialSum, lookAheadSum, total)
: doCut('b', vbox, partialSum, lookAheadSum, total); : doCut('b', vbox, partialSum, lookAheadSum, total);
} }
private static VBox[] doCut( private static VBox[] doCut(
char color, char color,
VBox vbox, VBox vbox,
int[] partialSum, int[] partialSum,
int[] lookAheadSum, int[] lookAheadSum,
int total int total
) { ) {
int vboxDim1; int vboxDim1;
int vboxDim2; int vboxDim2;
@ -508,7 +509,7 @@ final class ColorThief {
vbox2.b1 = d2 + 1; vbox2.b1 = d2 + 1;
} }
return new VBox[] {vbox1, vbox2}; return new VBox[]{vbox1, vbox2};
} }
} }