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

Add hsb/rgb format rendering for HSBType #3165

Merged
merged 1 commit into from Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -59,6 +59,9 @@ public class HSBType extends PercentType implements ComplexType, State, Command
private static final float RGB2XY[][] = { { 0.4124f, 0.3576f, 0.1805f }, { 0.2126f, 0.7152f, 0.0722f },
{ 0.0193f, 0.1192f, 0.9505f } };

private static final String UNIT_HSB = "%hsb%";
private static final String UNIT_RGB = "%rgb%";

protected BigDecimal hue;
protected BigDecimal saturation;

Expand Down Expand Up @@ -243,6 +246,21 @@ public String toFullString() {
return getHue() + "," + getSaturation() + "," + getBrightness();
}

@Override
public String format(String pattern) {
String formatPattern = pattern;
String val = getHue() + "," + getSaturation() + "," + getBrightness();
if (pattern.contains(UNIT_HSB)) {
formatPattern = pattern.replace(UNIT_HSB, "%s");
} else if (pattern.contains(UNIT_RGB)) {
formatPattern = pattern.replace(UNIT_RGB, "%s");
PercentType[] rgb = toRGB();
val = convertPercentToByte(rgb[0]) + "," + convertPercentToByte(rgb[1]) + ","
+ convertPercentToByte(rgb[2]);
}
return String.format(formatPattern, val);
}

@Override
public int hashCode() {
int tmp = 10000 * getHue().hashCode();
Expand Down
Expand Up @@ -41,6 +41,15 @@ public void testEquals() {
assertTrue(hsb1.equals(hsb2));
}

@Test
public void testFormat() {
HSBType hsb = new HSBType("316,69,47");

assertEquals("color 316,69,47", hsb.format("color %hsb%"));
assertEquals("color 119,37,97", hsb.format("color %rgb%"));
assertEquals("color 316,69,47", hsb.format("color %s"));
}

@Test
public void testHsbToRgbConversion() {
compareHsbToRgbValues("0,100,100", 255, 0, 0); // red
Expand Down