diff --git a/.gitignore b/.gitignore
index 2739029bb..742156b3d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -28,5 +28,3 @@ extensions/bin
target/
.metadata
-
-local.properties
diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md
index b1335e648..527c0dd10 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.6.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.4.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 f7c057b3e..0e902139c 100644
--- a/smack-experimental/build.gradle
+++ b/smack-experimental/build.gradle
@@ -12,5 +12,4 @@ 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 cac979d93..1226ce7dc 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-2019 Paul Schaub
+ * Copyright © 2018 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,17 +19,18 @@ 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.
@@ -48,17 +49,17 @@ public class ConsistentColor {
}
/**
- * Generate an angle in the HSLuv color space from the input string.
+ * Generate an angle in the CbCr plane from the input string.
* @see §5.1: Angle generation
*
* @param input input string
- * @return output angle in degrees
+ * @return output angle
*/
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 * 360;
+ return d * 2 * Math.PI;
}
/**
@@ -74,51 +75,84 @@ public class ConsistentColor {
case none:
break;
case redGreenBlindness:
- angle += 90;
- angle %= 180;
- angle += 270; // equivalent to -90 % 360, but eliminates negative results
- angle %= 360;
+ angle %= Math.PI;
break;
case blueBlindness:
- angle %= 180;
+ angle -= Math.PI / 2;
+ angle %= Math.PI;
+ angle += Math.PI / 2;
break;
}
return angle;
}
/**
- * Converting a HSLuv angle to RGB.
- * Saturation is set to 100 and lightness to 50, according to the XEP.
+ * Convert an angle in the CbCr plane to values cb, cr in the YCbCr color space.
+ * @see §5.3: CbCr generation
*
- * @param hue angle
- * @return rgb values between 0 and 1
- *
- * @see XEP-0392 §5.4: RGB generation
+ * @param angle angel in CbCr plane.
+ * @return value pair cb, cr
*/
- private static double[] hsluvToRgb(double hue) {
- return hsluvToRgb(hue, 100, 50);
+ 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};
}
/**
- * Converting a HSLuv angle to RGB.
+ * Convert a value pair cb, cr in the YCbCr color space to RGB.
+ * @see §5.4: CbCr to RGB
*
- * @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
+ * @param cbcr value pair from the YCbCr color space
+ * @return RGB value triple (R, G, B in [0,1])
*/
- private static double[] hsluvToRgb(double hue, double saturation, double lightness) {
- return HUSLColorConverter.hsluvToRgb(new double[] {hue, saturation, lightness});
+ 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[] 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]
- };
+ /**
+ * 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;
}
/**
@@ -155,54 +189,21 @@ public class ConsistentColor {
public static float[] RGBFrom(CharSequence input, ConsistentColorSettings settings) {
double angle = createAngle(input);
double correctedAngle = applyColorDeficiencyCorrection(angle, settings.getDeficiency());
- 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)
- };
+ double[] CbCr = angleToCbCr(correctedAngle);
+ float[] rgb = CbCrToRGB(CbCr, Y);
+ return rgb;
}
public static class ConsistentColorSettings {
private final Deficiency deficiency;
- private final float[] backgroundRGB;
public ConsistentColorSettings() {
- this.deficiency = Deficiency.none;
- this.backgroundRGB = null;
+ this(Deficiency.none);
}
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 3bf012d9d..612c2816a 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-2019 Paul Schaub
+ * Copyright © 2018 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,8 +17,10 @@
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;
@@ -32,95 +34,103 @@ 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.
+ Below tests check the test vectors from XEP-0392 §13.2.
*/
@Test
public void romeoNoDeficiencyTest() {
- float[] rgb = new float[] {0.865f, 0.000f, 0.686f};
- assertRGBEquals(rgb, ConsistentColor.RGBFrom(romeo), EPS);
+ String value = "Romeo";
+ float[] expected = new float[] {0.281f, 0.790f, 1.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
+ assertRGBEquals(expected, actual, EPS);
}
@Test
public void romeoRedGreenBlindnessTest() {
- float[] expected = new float[] {0.865f, 0.000f, 0.686f};
- float[] actual = ConsistentColor.RGBFrom(romeo, redGreenDeficiency);
+ String value = "Romeo";
+ float[] expected = new float[] {1.000f, 0.674f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void romeoBlueBlindnessTest() {
- float[] expected = new float[] {0.000f, 0.535f, 0.350f};
- float[] actual = ConsistentColor.RGBFrom(romeo, blueBlindnessDeficiency);
+ String value = "Romeo";
+ float[] expected = new float[] {1.000f, 0.674f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void julietNoDeficiencyTest() {
- float[] expected = new float[] {0.000f, 0.515f, 0.573f};
- float[] actual = ConsistentColor.RGBFrom(juliet, noDeficiency);
+ String value = "juliet@capulet.lit";
+ float[] expected = new float[] {0.337f, 1.000f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void julietRedGreenBlindnessTest() {
- float[] expected = new float[] {0.742f, 0.359f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(juliet, redGreenDeficiency);
+ String value = "juliet@capulet.lit";
+ float[] expected = new float[] {1.000f, 0.359f, 1.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void julietBlueBlindnessTest() {
- float[] expected = new float[] {0.742f, 0.359f, 0.000f};
- float[] actual = ConsistentColor.RGBFrom(juliet, blueBlindnessDeficiency);
+ String value = "juliet@capulet.lit";
+ float[] expected = new float[] {0.337f, 1.000f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void emojiNoDeficiencyTest() {
- float[] expected = new float[] {0.872f, 0.000f, 0.659f};
- float[] actual = ConsistentColor.RGBFrom(emoji, noDeficiency);
+ String value = "\uD83D\uDE3A";
+ float[] expected = new float[] {0.347f, 0.756f, 1.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void emojiRedGreenBlindnessTest() {
- float[] expected = new float[] {0.872f, 0.000f, 0.659f};
- float[] actual = ConsistentColor.RGBFrom(emoji, redGreenDeficiency);
+ String value = "\uD83D\uDE3A";
+ float[] expected = new float[] {1.000f, 0.708f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void emojiBlueBlindnessTest() {
- float[] expected = new float[] {0.000f, 0.533f, 0.373f};
- float[] actual = ConsistentColor.RGBFrom(emoji, blueBlindnessDeficiency);
+ String value = "\uD83D\uDE3A";
+ float[] expected = new float[] {1.000f, 0.708f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void councilNoDeficiencyTest() {
- float[] expected = new float[] {0.918f, 0.000f, 0.394f};
- float[] actual = ConsistentColor.RGBFrom(council, noDeficiency);
+ String value = "council";
+ float[] expected = new float[] {0.732f, 0.560f, 1.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void councilRedGreenBlindnessTest() {
- float[] expected = new float[] {0.918f, 0.000f, 0.394f};
- float[] actual = ConsistentColor.RGBFrom(council, redGreenDeficiency);
+ String value = "council";
+ float[] expected = new float[] {0.732f, 0.904f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@Test
public void councilBlueBlindnessTest() {
- float[] expected = new float[] {0.000f, 0.524f, 0.485f};
- float[] actual = ConsistentColor.RGBFrom(council, blueBlindnessDeficiency);
+ String value = "council";
+ float[] expected = new float[] {0.732f, 0.904f, 0.000f};
+ float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
@@ -136,7 +146,7 @@ public class ConsistentColorsTest extends SmackTestSuite {
assertEquals(3, actual.length);
for (int i = 0; i < actual.length; i++) {
- assertEquals(expected[i], actual[i], eps);
+ assertTrue(Math.abs(expected[i] - actual[i]) < eps);
}
}
}