diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md
index 527c0dd10..b1335e648 100644
--- a/documentation/extensions/index.md
+++ b/documentation/extensions/index.md
@@ -103,7 +103,7 @@ Experimental Smack Extensions and currently supported XEPs of smack-experimental
| [OpenPGP for XMPP: Instant Messaging](ox-im.md) | [XEP-0374](https://xmpp.org/extensions/xep-0374.html) | 0.2.0 | OpenPGP encrypted Instant Messaging. |
| [Spoiler Messages](spoiler.md) | [XEP-0382](https://xmpp.org/extensions/xep-0382.html) | 0.2.0 | Indicate that the body of a message should be treated as a spoiler. |
| [OMEMO Multi End Message and Object Encryption](omemo.md) | [XEP-0384](https://xmpp.org/extensions/xep-0384.html) | n/a | Encrypt messages using OMEMO encryption (currently only with smack-omemo-signal -> GPLv3). |
-| [Consistent Color Generation](consistent_colors.md) | [XEP-0392](https://xmpp.org/extensions/xep-0392.html) | 0.4.0 | Generate consistent colors for identifiers like usernames to provide a consistent user experience. |
+| [Consistent Color Generation](consistent_colors.md) | [XEP-0392](https://xmpp.org/extensions/xep-0392.html) | 0.6.0 | Generate consistent colors for identifiers like usernames to provide a consistent user experience. |
| [Message Markup](messagemarkup.md) | [XEP-0394](https://xmpp.org/extensions/xep-0394.html) | 0.1.0 | Style message bodies while keeping body and markup information separated. |
| DNS Queries over XMPP (DoX) | [XEP-0418](https://xmpp.org/extensions/xep-0418.html) | 0.1.0 | Send DNS queries and responses over XMPP. |
diff --git a/smack-experimental/build.gradle b/smack-experimental/build.gradle
index 0e902139c..f7c057b3e 100644
--- a/smack-experimental/build.gradle
+++ b/smack-experimental/build.gradle
@@ -12,4 +12,5 @@ dependencies {
testCompile project(path: ":smack-extensions", configuration: "testRuntime")
compile "org.bouncycastle:bcprov-jdk15on:$bouncyCastleVersion"
+ compile "org.hsluv:hsluv:0.2"
}
diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/colors/ConsistentColor.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/colors/ConsistentColor.java
index 1226ce7dc..cac979d93 100644
--- a/smack-experimental/src/main/java/org/jivesoftware/smackx/colors/ConsistentColor.java
+++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/colors/ConsistentColor.java
@@ -1,6 +1,6 @@
/**
*
- * Copyright © 2018 Paul Schaub
+ * Copyright © 2018-2019 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,18 +19,17 @@ package org.jivesoftware.smackx.colors;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.SHA1;
+import org.hsluv.HUSLColorConverter;
+
+/**
+ * Implementation of XEP-0392: Consistent Color Generation version 0.6.0.
+ *
+ * @author Paul Schaub
+ */
public class ConsistentColor {
private static final ConsistentColorSettings DEFAULT_SETTINGS = new ConsistentColorSettings();
- // See XEP-0392 §13.1 Constants for YCbCr (BT.601)
- private static final double KR = 0.299;
- private static final double KG = 0.587;
- private static final double KB = 0.114;
-
- // See XEP-0392 §5.4 CbCr to RGB
- private static final double Y = 0.732;
-
public enum Deficiency {
/**
* Do not apply measurements for color vision deficiency correction.
@@ -49,17 +48,17 @@ public class ConsistentColor {
}
/**
- * Generate an angle in the CbCr plane from the input string.
+ * Generate an angle in the HSLuv color space from the input string.
* @see §5.1: Angle generation
*
* @param input input string
- * @return output angle
+ * @return output angle in degrees
*/
private static double createAngle(CharSequence input) {
byte[] h = SHA1.bytes(input.toString());
double v = u(h[0]) + (256 * u(h[1]));
double d = v / 65536;
- return d * 2 * Math.PI;
+ return d * 360;
}
/**
@@ -75,84 +74,51 @@ public class ConsistentColor {
case none:
break;
case redGreenBlindness:
- angle %= Math.PI;
+ angle += 90;
+ angle %= 180;
+ angle += 270; // equivalent to -90 % 360, but eliminates negative results
+ angle %= 360;
break;
case blueBlindness:
- angle -= Math.PI / 2;
- angle %= Math.PI;
- angle += Math.PI / 2;
+ angle %= 180;
break;
}
return angle;
}
/**
- * Convert an angle in the CbCr plane to values cb, cr in the YCbCr color space.
- * @see §5.3: CbCr generation
+ * Converting a HSLuv angle to RGB.
+ * Saturation is set to 100 and lightness to 50, according to the XEP.
*
- * @param angle angel in CbCr plane.
- * @return value pair cb, cr
+ * @param hue angle
+ * @return rgb values between 0 and 1
+ *
+ * @see XEP-0392 §5.4: RGB generation
*/
- private static double[] angleToCbCr(double angle) {
- double cb = Math.cos(angle);
- double cr = Math.sin(angle);
-
- double acb = Math.abs(cb);
- double acr = Math.abs(cr);
- double factor;
- if (acr > acb) {
- factor = 0.5 / acr;
- } else {
- factor = 0.5 / acb;
- }
-
- cb *= factor;
- cr *= factor;
-
- return new double[] {cb, cr};
+ private static double[] hsluvToRgb(double hue) {
+ return hsluvToRgb(hue, 100, 50);
}
/**
- * Convert a value pair cb, cr in the YCbCr color space to RGB.
- * @see §5.4: CbCr to RGB
+ * Converting a HSLuv angle to RGB.
*
- * @param cbcr value pair from the YCbCr color space
- * @return RGB value triple (R, G, B in [0,1])
+ * @param hue angle 0 <= hue < 360
+ * @param saturation saturation 0 <= saturation <= 100
+ * @param lightness lightness 0 <= lightness <= 100
+ * @return rbg array with values 0 <= (r,g,b) <= 1
+ *
+ * @see HSL to RGB conversion
*/
- private static float[] CbCrToRGB(double[] cbcr, double y) {
- double cb = cbcr[0];
- double cr = cbcr[1];
-
- double r = 2 * (1 - KR) * cr + y;
- double b = 2 * (1 - KB) * cb + y;
- double g = (y - KR * r - KB * b) / KG;
-
- // Clip values to [0,1]
- r = clip(r);
- g = clip(g);
- b = clip(b);
-
- return new float[] {(float) r, (float) g, (float) b};
+ private static double[] hsluvToRgb(double hue, double saturation, double lightness) {
+ return HUSLColorConverter.hsluvToRgb(new double[] {hue, saturation, lightness});
}
- /**
- * Clip values to stay in range(0,1).
- *
- * @param value input
- * @return input clipped to stay in boundaries from 0 to 1.
- */
- private static double clip(double value) {
- double out = value;
-
- if (value < 0) {
- out = 0;
- }
-
- if (value > 1) {
- out = 1;
- }
-
- return out;
+ private static double[] mixWithBackground(double[] rgbi, float[] rgbb) {
+ return new double[] {
+ 0.2 * (1 - rgbb[0]) + 0.8 * rgbi[0],
+ 0.2 * (1 - rgbb[1]) + 0.8 * rgbi[1],
+ 0.2 * (1 - rgbb[2]) + 0.8 * rgbi[2]
+ };
}
/**
@@ -189,21 +155,54 @@ public class ConsistentColor {
public static float[] RGBFrom(CharSequence input, ConsistentColorSettings settings) {
double angle = createAngle(input);
double correctedAngle = applyColorDeficiencyCorrection(angle, settings.getDeficiency());
- double[] CbCr = angleToCbCr(correctedAngle);
- float[] rgb = CbCrToRGB(CbCr, Y);
- return rgb;
+ double[] rgb = hsluvToRgb(correctedAngle);
+ if (settings.backgroundRGB != null) {
+ rgb = mixWithBackground(rgb, settings.backgroundRGB);
+ }
+
+ return new float[] {(float) rgb[0], (float) rgb[1], (float) rgb[2]};
+ }
+
+ public static int[] floatRgbToInts(float[] floats) {
+ return new int[] {
+ (int) (floats[0] * 255),
+ (int) (floats[1] * 255),
+ (int) (floats[2] * 255)
+ };
}
public static class ConsistentColorSettings {
private final Deficiency deficiency;
+ private final float[] backgroundRGB;
public ConsistentColorSettings() {
- this(Deficiency.none);
+ this.deficiency = Deficiency.none;
+ this.backgroundRGB = null;
}
public ConsistentColorSettings(Deficiency deficiency) {
this.deficiency = Objects.requireNonNull(deficiency, "Deficiency must be given");
+ this.backgroundRGB = null;
+ }
+
+ public ConsistentColorSettings(Deficiency deficiency,
+ float[] backgroundRGB) {
+ this.deficiency = Objects.requireNonNull(deficiency, "Deficiency must be given");
+ if (backgroundRGB.length != 3) {
+ throw new IllegalArgumentException("Background RGB value array must have length 3.");
+ }
+
+ for (float f : backgroundRGB) {
+ checkRange(f, 0, 1);
+ }
+ this.backgroundRGB = backgroundRGB;
+ }
+
+ private static void checkRange(float value, float lower, float upper) {
+ if (lower > value || upper < value) {
+ throw new IllegalArgumentException("Value out of range.");
+ }
}
/**
diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/colors/ConsistentColorsTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/colors/ConsistentColorsTest.java
index 612c2816a..3bf012d9d 100644
--- a/smack-experimental/src/test/java/org/jivesoftware/smackx/colors/ConsistentColorsTest.java
+++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/colors/ConsistentColorsTest.java
@@ -1,6 +1,6 @@
/**
*
- * Copyright © 2018 Paul Schaub
+ * Copyright © 2018-2019 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,10 +17,8 @@
package org.jivesoftware.smackx.colors;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import org.jivesoftware.smack.test.util.SmackTestSuite;
-
import org.jivesoftware.smackx.colors.ConsistentColor.Deficiency;
import org.junit.jupiter.api.Test;
@@ -34,103 +32,95 @@ public class ConsistentColorsTest extends SmackTestSuite {
private static final ConsistentColor.ConsistentColorSettings redGreenDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.redGreenBlindness);
private static final ConsistentColor.ConsistentColorSettings blueBlindnessDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.blueBlindness);
+ private static final String romeo = "Romeo";
+ private static final String juliet = "juliet@capulet.lit";
+ private static final String emoji = "\uD83D\uDE3A";
+ private static final String council = "council";
+
/*
- Below tests check the test vectors from XEP-0392 §13.2.
+ Below tests check the test vectors from XEP-0392 §13.
*/
@Test
public void romeoNoDeficiencyTest() {
- String value = "Romeo";
- float[] expected = new float[] {0.281f, 0.790f, 1.000f};
- float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
- assertRGBEquals(expected, actual, EPS);
+ float[] rgb = new float[] {0.865f, 0.000f, 0.686f};
+ assertRGBEquals(rgb, ConsistentColor.RGBFrom(romeo), EPS);
}
@Test
public void romeoRedGreenBlindnessTest() {
- String value = "Romeo";
- float[] expected = new float[] {1.000f, 0.674f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
+ float[] expected = new float[] {0.865f, 0.000f, 0.686f};
+ float[] actual = ConsistentColor.RGBFrom(romeo, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void romeoBlueBlindnessTest() {
- String value = "Romeo";
- float[] expected = new float[] {1.000f, 0.674f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
+ float[] expected = new float[] {0.000f, 0.535f, 0.350f};
+ float[] actual = ConsistentColor.RGBFrom(romeo, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void julietNoDeficiencyTest() {
- String value = "juliet@capulet.lit";
- float[] expected = new float[] {0.337f, 1.000f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
+ float[] expected = new float[] {0.000f, 0.515f, 0.573f};
+ float[] actual = ConsistentColor.RGBFrom(juliet, noDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void julietRedGreenBlindnessTest() {
- String value = "juliet@capulet.lit";
- float[] expected = new float[] {1.000f, 0.359f, 1.000f};
- float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
+ float[] expected = new float[] {0.742f, 0.359f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(juliet, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void julietBlueBlindnessTest() {
- String value = "juliet@capulet.lit";
- float[] expected = new float[] {0.337f, 1.000f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
+ float[] expected = new float[] {0.742f, 0.359f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(juliet, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void emojiNoDeficiencyTest() {
- String value = "\uD83D\uDE3A";
- float[] expected = new float[] {0.347f, 0.756f, 1.000f};
- float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
+ float[] expected = new float[] {0.872f, 0.000f, 0.659f};
+ float[] actual = ConsistentColor.RGBFrom(emoji, noDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void emojiRedGreenBlindnessTest() {
- String value = "\uD83D\uDE3A";
- float[] expected = new float[] {1.000f, 0.708f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
+ float[] expected = new float[] {0.872f, 0.000f, 0.659f};
+ float[] actual = ConsistentColor.RGBFrom(emoji, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void emojiBlueBlindnessTest() {
- String value = "\uD83D\uDE3A";
- float[] expected = new float[] {1.000f, 0.708f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
+ float[] expected = new float[] {0.000f, 0.533f, 0.373f};
+ float[] actual = ConsistentColor.RGBFrom(emoji, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void councilNoDeficiencyTest() {
- String value = "council";
- float[] expected = new float[] {0.732f, 0.560f, 1.000f};
- float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
+ float[] expected = new float[] {0.918f, 0.000f, 0.394f};
+ float[] actual = ConsistentColor.RGBFrom(council, noDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void councilRedGreenBlindnessTest() {
- String value = "council";
- float[] expected = new float[] {0.732f, 0.904f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
+ float[] expected = new float[] {0.918f, 0.000f, 0.394f};
+ float[] actual = ConsistentColor.RGBFrom(council, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void councilBlueBlindnessTest() {
- String value = "council";
- float[] expected = new float[] {0.732f, 0.904f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
+ float[] expected = new float[] {0.000f, 0.524f, 0.485f};
+ float[] actual = ConsistentColor.RGBFrom(council, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@@ -146,7 +136,7 @@ public class ConsistentColorsTest extends SmackTestSuite {
assertEquals(3, actual.length);
for (int i = 0; i < actual.length; i++) {
- assertTrue(Math.abs(expected[i] - actual[i]) < eps);
+ assertEquals(expected[i], actual[i], eps);
}
}
}