Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ColorUtil] optimise constants; hue overflow check #3629

Merged
merged 2 commits into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@
public class ColorUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ColorUtil.class);
private static final MathContext COLOR_MATH_CONTEXT = new MathContext(5, RoundingMode.HALF_UP);
protected static final BigDecimal BIG_DECIMAL_HUNDRED = BigDecimal.valueOf(100);
private static final BigDecimal BIG_DECIMAL_360 = BigDecimal.valueOf(360);
private static final BigDecimal BIG_DECIMAL_255 = BigDecimal.valueOf(255);
private static final BigDecimal BIG_DECIMAL_240 = BigDecimal.valueOf(240);
private static final BigDecimal BIG_DECIMAL_120 = BigDecimal.valueOf(120);
private static final BigDecimal BIG_DECIMAL_100 = BigDecimal.valueOf(100);
private static final BigDecimal BIG_DECIMAL_60 = BigDecimal.valueOf(60);
private static final BigDecimal BIG_DECIMAL_5 = BigDecimal.valueOf(5);
private static final BigDecimal BIG_DECIMAL_3 = BigDecimal.valueOf(3);
private static final BigDecimal BIG_DECIMAL_2_POINT_55 = new BigDecimal("2.55");

public static final Gamut DEFAULT_GAMUT = new Gamut(new double[] { 0.9961, 0.0001 }, new double[] { 0, 0.9961 },
new double[] { 0, 0.0001 });

Expand Down Expand Up @@ -80,11 +89,11 @@ public static PercentType[] hsbToRgbPercent(HSBType hsb) {
PercentType green = null;
PercentType blue = null;

final BigDecimal h = hsb.getHue().toBigDecimal().divide(BIG_DECIMAL_HUNDRED, 10, RoundingMode.HALF_UP);
final BigDecimal s = hsb.getSaturation().toBigDecimal().divide(BIG_DECIMAL_HUNDRED);
final BigDecimal h = hsb.getHue().toBigDecimal().divide(BIG_DECIMAL_100, 10, RoundingMode.HALF_UP);
final BigDecimal s = hsb.getSaturation().toBigDecimal().divide(BIG_DECIMAL_100);

int hInt = h.multiply(BigDecimal.valueOf(5)).divide(BigDecimal.valueOf(3), 0, RoundingMode.DOWN).intValue();
final BigDecimal f = h.multiply(BigDecimal.valueOf(5)).divide(BigDecimal.valueOf(3), 10, RoundingMode.HALF_UP)
int hInt = h.multiply(BIG_DECIMAL_5).divide(BIG_DECIMAL_3, 0, RoundingMode.DOWN).intValue();
final BigDecimal f = h.multiply(BIG_DECIMAL_5).divide(BIG_DECIMAL_3, 10, RoundingMode.HALF_UP)
.remainder(BigDecimal.ONE);
final BigDecimal value = hsb.getBrightness().toBigDecimal();

Expand Down Expand Up @@ -241,10 +250,10 @@ public static HSBType rgbToHsb(PercentType[] rgb) throws IllegalArgumentExceptio
return new HSBType(new DecimalType(), new PercentType(), new PercentType(max));
}

PercentType saturation = new PercentType(span.divide(max, COLOR_MATH_CONTEXT).multiply(BIG_DECIMAL_HUNDRED));
PercentType saturation = new PercentType(span.divide(max, COLOR_MATH_CONTEXT).multiply(BIG_DECIMAL_100));
PercentType brightness = new PercentType(max);

BigDecimal scale = span.divide(BigDecimal.valueOf(60), COLOR_MATH_CONTEXT);
BigDecimal scale = span.divide(BIG_DECIMAL_60, COLOR_MATH_CONTEXT);

BigDecimal redAngle = max.subtract(r).divide(scale, COLOR_MATH_CONTEXT);
BigDecimal greenAngle = max.subtract(g).divide(scale, COLOR_MATH_CONTEXT);
Expand All @@ -254,12 +263,14 @@ public static HSBType rgbToHsb(PercentType[] rgb) throws IllegalArgumentExceptio
if (r.compareTo(max) == 0) {
hue = blueAngle.subtract(greenAngle);
} else if (g.compareTo(max) == 0) {
hue = BigDecimal.valueOf(120).add(redAngle).subtract(blueAngle);
hue = BIG_DECIMAL_120.add(redAngle).subtract(blueAngle);
} else {
hue = BigDecimal.valueOf(240).add(greenAngle).subtract(redAngle);
hue = BIG_DECIMAL_240.add(greenAngle).subtract(redAngle);
}
if (hue.compareTo(BigDecimal.ZERO) < 0) {
hue = hue.add(BigDecimal.valueOf(360));
hue = hue.add(BIG_DECIMAL_360);
} else if (hue.compareTo(BIG_DECIMAL_360) > 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@J-N-K just re-reading this line, I think it should be >= rather than > to cover the exact edge case where hue is 360. Or??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it's fine. If that is the case 360-360=0 which is the same color.

hue = hue.subtract(BIG_DECIMAL_360);
}

return new HSBType(new DecimalType(hue), saturation, brightness);
Expand Down Expand Up @@ -485,15 +496,15 @@ private static boolean inRange(double val) {
}

private static int convertColorPercentToByte(PercentType percent) {
return percent.toBigDecimal().multiply(BigDecimal.valueOf(255))
.divide(BIG_DECIMAL_HUNDRED, 0, RoundingMode.HALF_UP).intValue();
return percent.toBigDecimal().multiply(BIG_DECIMAL_255).divide(BIG_DECIMAL_100, 0, RoundingMode.HALF_UP)
.intValue();
}

private static PercentType convertByteToColorPercent(int b) {
return new PercentType(new BigDecimal(b).divide(new BigDecimal("2.55"), COLOR_MATH_CONTEXT));
return new PercentType(new BigDecimal(b).divide(BIG_DECIMAL_2_POINT_55, COLOR_MATH_CONTEXT));
}

private static PercentType convertDoubleToColorPercent(double d) {
return new PercentType(new BigDecimal(d).multiply(BIG_DECIMAL_HUNDRED, COLOR_MATH_CONTEXT));
return new PercentType(new BigDecimal(d).multiply(BIG_DECIMAL_100, COLOR_MATH_CONTEXT));
}
}