1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-16 01:02:06 +01:00

Compare commits

..

No commits in common. "d9f45e03f3cc5b55db3fcc48e8440c6cb9b641a1" and "c6c904cc3eb9409641b4ad334851debeb8e3b492" have entirely different histories.

5 changed files with 120 additions and 112 deletions

2
.gitignore vendored
View file

@ -28,5 +28,3 @@ extensions/bin
target/ target/
.metadata .metadata
local.properties

View file

@ -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. | | [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. | | [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). | | [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. | | [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. | | DNS Queries over XMPP (DoX) | [XEP-0418](https://xmpp.org/extensions/xep-0418.html) | 0.1.0 | Send DNS queries and responses over XMPP. |

View file

@ -12,5 +12,4 @@ dependencies {
testCompile project(path: ":smack-extensions", configuration: "testRuntime") testCompile project(path: ":smack-extensions", configuration: "testRuntime")
compile "org.bouncycastle:bcprov-jdk15on:$bouncyCastleVersion" compile "org.bouncycastle:bcprov-jdk15on:$bouncyCastleVersion"
compile "org.hsluv:hsluv:0.2"
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2018-2019 Paul Schaub * Copyright © 2018 Paul Schaub
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Objects;
import org.jivesoftware.smack.util.SHA1; 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 { public class ConsistentColor {
private static final ConsistentColorSettings DEFAULT_SETTINGS = new ConsistentColorSettings(); 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 { public enum Deficiency {
/** /**
* Do not apply measurements for color vision deficiency correction. * 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 <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-angle">§5.1: Angle generation</a> * @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-angle">§5.1: Angle generation</a>
* *
* @param input input string * @param input input string
* @return output angle in degrees * @return output angle
*/ */
private static double createAngle(CharSequence input) { private static double createAngle(CharSequence input) {
byte[] h = SHA1.bytes(input.toString()); byte[] h = SHA1.bytes(input.toString());
double v = u(h[0]) + (256 * u(h[1])); double v = u(h[0]) + (256 * u(h[1]));
double d = v / 65536; double d = v / 65536;
return d * 360; return d * 2 * Math.PI;
} }
/** /**
@ -74,51 +75,84 @@ public class ConsistentColor {
case none: case none:
break; break;
case redGreenBlindness: case redGreenBlindness:
angle += 90; angle %= Math.PI;
angle %= 180;
angle += 270; // equivalent to -90 % 360, but eliminates negative results
angle %= 360;
break; break;
case blueBlindness: case blueBlindness:
angle %= 180; angle -= Math.PI / 2;
angle %= Math.PI;
angle += Math.PI / 2;
break; break;
} }
return angle; return angle;
} }
/** /**
* Converting a HSLuv angle to RGB. * Convert an angle in the CbCr plane to values cb, cr in the YCbCr color space.
* Saturation is set to 100 and lightness to 50, according to the XEP. * @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-cbcr">§5.3: CbCr generation</a>
* *
* @param hue angle * @param angle angel in CbCr plane.
* @return rgb values between 0 and 1 * @return value pair cb, cr
*
* @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-rgb">XEP-0392 §5.4: RGB generation</a>
*/ */
private static double[] hsluvToRgb(double hue) { private static double[] angleToCbCr(double angle) {
return hsluvToRgb(hue, 100, 50); 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 <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-rgb">§5.4: CbCr to RGB</a>
* *
* @param hue angle 0 <= hue < 360 * @param cbcr value pair from the YCbCr color space
* @param saturation saturation 0 <= saturation <= 100 * @return RGB value triple (R, G, B in [0,1])
* @param lightness lightness 0 <= lightness <= 100
* @return rbg array with values 0 <= (r,g,b) <= 1
*
* @see <a href="https://www.rapidtables.com/convert/color/hsl-to-rgb.html">HSL to RGB conversion</a>
*/ */
private static double[] hsluvToRgb(double hue, double saturation, double lightness) { private static float[] CbCrToRGB(double[] cbcr, double y) {
return HUSLColorConverter.hsluvToRgb(new double[] {hue, saturation, lightness}); 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[] { * Clip values to stay in range(0,1).
0.2 * (1 - rgbb[0]) + 0.8 * rgbi[0], *
0.2 * (1 - rgbb[1]) + 0.8 * rgbi[1], * @param value input
0.2 * (1 - rgbb[2]) + 0.8 * rgbi[2] * @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) { public static float[] RGBFrom(CharSequence input, ConsistentColorSettings settings) {
double angle = createAngle(input); double angle = createAngle(input);
double correctedAngle = applyColorDeficiencyCorrection(angle, settings.getDeficiency()); double correctedAngle = applyColorDeficiencyCorrection(angle, settings.getDeficiency());
double[] rgb = hsluvToRgb(correctedAngle); double[] CbCr = angleToCbCr(correctedAngle);
if (settings.backgroundRGB != null) { float[] rgb = CbCrToRGB(CbCr, Y);
rgb = mixWithBackground(rgb, settings.backgroundRGB); return rgb;
}
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 { public static class ConsistentColorSettings {
private final Deficiency deficiency; private final Deficiency deficiency;
private final float[] backgroundRGB;
public ConsistentColorSettings() { public ConsistentColorSettings() {
this.deficiency = Deficiency.none; this(Deficiency.none);
this.backgroundRGB = null;
} }
public ConsistentColorSettings(Deficiency deficiency) { public ConsistentColorSettings(Deficiency deficiency) {
this.deficiency = Objects.requireNonNull(deficiency, "Deficiency must be given"); 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.");
}
} }
/** /**

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2018-2019 Paul Schaub * Copyright © 2018 Paul Schaub
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,8 +17,10 @@
package org.jivesoftware.smackx.colors; package org.jivesoftware.smackx.colors;
import static org.junit.jupiter.api.Assertions.assertEquals; 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.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.colors.ConsistentColor.Deficiency; import org.jivesoftware.smackx.colors.ConsistentColor.Deficiency;
import org.junit.jupiter.api.Test; 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 redGreenDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.redGreenBlindness);
private static final ConsistentColor.ConsistentColorSettings blueBlindnessDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.blueBlindness); 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 @Test
public void romeoNoDeficiencyTest() { public void romeoNoDeficiencyTest() {
float[] rgb = new float[] {0.865f, 0.000f, 0.686f}; String value = "Romeo";
assertRGBEquals(rgb, ConsistentColor.RGBFrom(romeo), EPS); float[] expected = new float[] {0.281f, 0.790f, 1.000f};
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void romeoRedGreenBlindnessTest() { public void romeoRedGreenBlindnessTest() {
float[] expected = new float[] {0.865f, 0.000f, 0.686f}; String value = "Romeo";
float[] actual = ConsistentColor.RGBFrom(romeo, redGreenDeficiency); float[] expected = new float[] {1.000f, 0.674f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void romeoBlueBlindnessTest() { public void romeoBlueBlindnessTest() {
float[] expected = new float[] {0.000f, 0.535f, 0.350f}; String value = "Romeo";
float[] actual = ConsistentColor.RGBFrom(romeo, blueBlindnessDeficiency); float[] expected = new float[] {1.000f, 0.674f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void julietNoDeficiencyTest() { public void julietNoDeficiencyTest() {
float[] expected = new float[] {0.000f, 0.515f, 0.573f}; String value = "juliet@capulet.lit";
float[] actual = ConsistentColor.RGBFrom(juliet, noDeficiency); float[] expected = new float[] {0.337f, 1.000f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void julietRedGreenBlindnessTest() { public void julietRedGreenBlindnessTest() {
float[] expected = new float[] {0.742f, 0.359f, 0.000f}; String value = "juliet@capulet.lit";
float[] actual = ConsistentColor.RGBFrom(juliet, redGreenDeficiency); float[] expected = new float[] {1.000f, 0.359f, 1.000f};
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void julietBlueBlindnessTest() { public void julietBlueBlindnessTest() {
float[] expected = new float[] {0.742f, 0.359f, 0.000f}; String value = "juliet@capulet.lit";
float[] actual = ConsistentColor.RGBFrom(juliet, blueBlindnessDeficiency); float[] expected = new float[] {0.337f, 1.000f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void emojiNoDeficiencyTest() { public void emojiNoDeficiencyTest() {
float[] expected = new float[] {0.872f, 0.000f, 0.659f}; String value = "\uD83D\uDE3A";
float[] actual = ConsistentColor.RGBFrom(emoji, noDeficiency); float[] expected = new float[] {0.347f, 0.756f, 1.000f};
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void emojiRedGreenBlindnessTest() { public void emojiRedGreenBlindnessTest() {
float[] expected = new float[] {0.872f, 0.000f, 0.659f}; String value = "\uD83D\uDE3A";
float[] actual = ConsistentColor.RGBFrom(emoji, redGreenDeficiency); float[] expected = new float[] {1.000f, 0.708f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void emojiBlueBlindnessTest() { public void emojiBlueBlindnessTest() {
float[] expected = new float[] {0.000f, 0.533f, 0.373f}; String value = "\uD83D\uDE3A";
float[] actual = ConsistentColor.RGBFrom(emoji, blueBlindnessDeficiency); float[] expected = new float[] {1.000f, 0.708f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void councilNoDeficiencyTest() { public void councilNoDeficiencyTest() {
float[] expected = new float[] {0.918f, 0.000f, 0.394f}; String value = "council";
float[] actual = ConsistentColor.RGBFrom(council, noDeficiency); float[] expected = new float[] {0.732f, 0.560f, 1.000f};
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void councilRedGreenBlindnessTest() { public void councilRedGreenBlindnessTest() {
float[] expected = new float[] {0.918f, 0.000f, 0.394f}; String value = "council";
float[] actual = ConsistentColor.RGBFrom(council, redGreenDeficiency); float[] expected = new float[] {0.732f, 0.904f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void councilBlueBlindnessTest() { public void councilBlueBlindnessTest() {
float[] expected = new float[] {0.000f, 0.524f, 0.485f}; String value = "council";
float[] actual = ConsistentColor.RGBFrom(council, blueBlindnessDeficiency); float[] expected = new float[] {0.732f, 0.904f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@ -136,7 +146,7 @@ public class ConsistentColorsTest extends SmackTestSuite {
assertEquals(3, actual.length); assertEquals(3, actual.length);
for (int i = 0; i < actual.length; i++) { for (int i = 0; i < actual.length; i++) {
assertEquals(expected[i], actual[i], eps); assertTrue(Math.abs(expected[i] - actual[i]) < eps);
} }
} }
} }