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

Merge branch 'master' of github.com:igniterealtime/Smack

This commit is contained in:
Florian Schmaus 2019-07-04 16:57:07 +02:00
commit 178ae8abef
5 changed files with 112 additions and 120 deletions

2
.gitignore vendored
View file

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

View file

@ -104,7 +104,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.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. | | [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,4 +12,5 @@ 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 Paul Schaub * Copyright © 2018-2019 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,18 +19,17 @@ 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.
@ -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 <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 * @return output angle in degrees
*/ */
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 * 2 * Math.PI; return d * 360;
} }
/** /**
@ -75,84 +74,51 @@ public class ConsistentColor {
case none: case none:
break; break;
case redGreenBlindness: case redGreenBlindness:
angle %= Math.PI; angle += 90;
angle %= 180;
angle += 270; // equivalent to -90 % 360, but eliminates negative results
angle %= 360;
break; break;
case blueBlindness: case blueBlindness:
angle -= Math.PI / 2; angle %= 180;
angle %= Math.PI;
angle += Math.PI / 2;
break; break;
} }
return angle; return angle;
} }
/** /**
* Convert an angle in the CbCr plane to values cb, cr in the YCbCr color space. * Converting a HSLuv angle to RGB.
* @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-cbcr">§5.3: CbCr generation</a> * Saturation is set to 100 and lightness to 50, according to the XEP.
* *
* @param angle angel in CbCr plane. * @param hue angle
* @return value pair cb, cr * @return rgb values between 0 and 1
*
* @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-rgb">XEP-0392 §5.4: RGB generation</a>
*/ */
private static double[] angleToCbCr(double angle) { private static double[] hsluvToRgb(double hue) {
double cb = Math.cos(angle); return hsluvToRgb(hue, 100, 50);
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};
} }
/** /**
* Convert a value pair cb, cr in the YCbCr color space to RGB. * Converting a HSLuv angle to RGB.
* @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-rgb">§5.4: CbCr to RGB</a>
* *
* @param cbcr value pair from the YCbCr color space * @param hue angle 0 <= hue < 360
* @return RGB value triple (R, G, B in [0,1]) * @param saturation saturation 0 <= saturation <= 100
* @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 float[] CbCrToRGB(double[] cbcr, double y) { private static double[] hsluvToRgb(double hue, double saturation, double lightness) {
double cb = cbcr[0]; return HUSLColorConverter.hsluvToRgb(new double[] {hue, saturation, lightness});
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) {
* Clip values to stay in range(0,1). return new double[] {
* 0.2 * (1 - rgbb[0]) + 0.8 * rgbi[0],
* @param value input 0.2 * (1 - rgbb[1]) + 0.8 * rgbi[1],
* @return input clipped to stay in boundaries from 0 to 1. 0.2 * (1 - rgbb[2]) + 0.8 * rgbi[2]
*/ };
private static double clip(double value) {
double out = value;
if (value < 0) {
out = 0;
}
if (value > 1) {
out = 1;
}
return out;
} }
/** /**
@ -189,21 +155,54 @@ 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[] CbCr = angleToCbCr(correctedAngle); double[] rgb = hsluvToRgb(correctedAngle);
float[] rgb = CbCrToRGB(CbCr, Y); if (settings.backgroundRGB != null) {
return rgb; 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 { public static class ConsistentColorSettings {
private final Deficiency deficiency; private final Deficiency deficiency;
private final float[] backgroundRGB;
public ConsistentColorSettings() { public ConsistentColorSettings() {
this(Deficiency.none); this.deficiency = 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 Paul Schaub * Copyright © 2018-2019 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,10 +17,8 @@
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;
@ -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 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.2. Below tests check the test vectors from XEP-0392 §13.
*/ */
@Test @Test
public void romeoNoDeficiencyTest() { public void romeoNoDeficiencyTest() {
String value = "Romeo"; float[] rgb = new float[] {0.865f, 0.000f, 0.686f};
float[] expected = new float[] {0.281f, 0.790f, 1.000f}; assertRGBEquals(rgb, ConsistentColor.RGBFrom(romeo), EPS);
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void romeoRedGreenBlindnessTest() { public void romeoRedGreenBlindnessTest() {
String value = "Romeo"; float[] expected = new float[] {0.865f, 0.000f, 0.686f};
float[] expected = new float[] {1.000f, 0.674f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(romeo, redGreenDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void romeoBlueBlindnessTest() { public void romeoBlueBlindnessTest() {
String value = "Romeo"; float[] expected = new float[] {0.000f, 0.535f, 0.350f};
float[] expected = new float[] {1.000f, 0.674f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(romeo, blueBlindnessDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void julietNoDeficiencyTest() { public void julietNoDeficiencyTest() {
String value = "juliet@capulet.lit"; float[] expected = new float[] {0.000f, 0.515f, 0.573f};
float[] expected = new float[] {0.337f, 1.000f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(juliet, noDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void julietRedGreenBlindnessTest() { public void julietRedGreenBlindnessTest() {
String value = "juliet@capulet.lit"; float[] expected = new float[] {0.742f, 0.359f, 0.000f};
float[] expected = new float[] {1.000f, 0.359f, 1.000f}; float[] actual = ConsistentColor.RGBFrom(juliet, redGreenDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void julietBlueBlindnessTest() { public void julietBlueBlindnessTest() {
String value = "juliet@capulet.lit"; float[] expected = new float[] {0.742f, 0.359f, 0.000f};
float[] expected = new float[] {0.337f, 1.000f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(juliet, blueBlindnessDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void emojiNoDeficiencyTest() { public void emojiNoDeficiencyTest() {
String value = "\uD83D\uDE3A"; float[] expected = new float[] {0.872f, 0.000f, 0.659f};
float[] expected = new float[] {0.347f, 0.756f, 1.000f}; float[] actual = ConsistentColor.RGBFrom(emoji, noDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void emojiRedGreenBlindnessTest() { public void emojiRedGreenBlindnessTest() {
String value = "\uD83D\uDE3A"; float[] expected = new float[] {0.872f, 0.000f, 0.659f};
float[] expected = new float[] {1.000f, 0.708f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(emoji, redGreenDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void emojiBlueBlindnessTest() { public void emojiBlueBlindnessTest() {
String value = "\uD83D\uDE3A"; float[] expected = new float[] {0.000f, 0.533f, 0.373f};
float[] expected = new float[] {1.000f, 0.708f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(emoji, blueBlindnessDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void councilNoDeficiencyTest() { public void councilNoDeficiencyTest() {
String value = "council"; float[] expected = new float[] {0.918f, 0.000f, 0.394f};
float[] expected = new float[] {0.732f, 0.560f, 1.000f}; float[] actual = ConsistentColor.RGBFrom(council, noDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, noDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void councilRedGreenBlindnessTest() { public void councilRedGreenBlindnessTest() {
String value = "council"; float[] expected = new float[] {0.918f, 0.000f, 0.394f};
float[] expected = new float[] {0.732f, 0.904f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(council, redGreenDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, redGreenDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@Test @Test
public void councilBlueBlindnessTest() { public void councilBlueBlindnessTest() {
String value = "council"; float[] expected = new float[] {0.000f, 0.524f, 0.485f};
float[] expected = new float[] {0.732f, 0.904f, 0.000f}; float[] actual = ConsistentColor.RGBFrom(council, blueBlindnessDeficiency);
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS); assertRGBEquals(expected, actual, EPS);
} }
@ -146,7 +136,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++) {
assertTrue(Math.abs(expected[i] - actual[i]) < eps); assertEquals(expected[i], actual[i], eps);
} }
} }
} }