1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 09:09:38 +02:00

Merge branch '4.2'

This commit is contained in:
Florian Schmaus 2018-02-21 20:13:05 +01:00
commit a48e8ef843
155 changed files with 638 additions and 160 deletions

View file

@ -16,6 +16,6 @@
*/
/**
* Smack's API for XEP-280: Message Carbons.
* Smack's API for XEP-0280: Message Carbons.
*/
package org.jivesoftware.smackx.carbons;

View file

@ -16,6 +16,6 @@
*/
/**
* Stanzas and extension elements for XEP-280: Message Carbons.
* Stanzas and extension elements for XEP-0280: Message Carbons.
*/
package org.jivesoftware.smackx.carbons.packet;

View file

@ -16,6 +16,6 @@
*/
/**
* Provider for XEP-280: Message Carbons.
* Provider for XEP-0280: Message Carbons.
*/
package org.jivesoftware.smackx.carbons.provider;

View file

@ -0,0 +1,218 @@
/**
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.colors;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.SHA1;
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.
*/
none,
/**
* Activate color correction for users suffering from red-green-blindness.
*/
redGreenBlindness,
/**
* Activate color correction for users suffering from blue-blindness.
*/
blueBlindness
}
/**
* 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>
*
* @param input input string
* @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 * 2 * Math.PI;
}
/**
* Apply correction for color vision deficiencies to an angle in the CbCr plane.
* @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-cvd">§5.2: Corrections for Color Vision Deficiencies</a>
*
* @param angle angle in CbCr plane
* @param deficiency type of vision deficiency
* @return corrected angle in CbCr plane
*/
private static double applyColorDeficiencyCorrection(double angle, Deficiency deficiency) {
switch (deficiency) {
case none:
break;
case redGreenBlindness:
angle %= Math.PI;
break;
case blueBlindness:
angle -= Math.PI / 2;
angle %= Math.PI;
angle += Math.PI / 2;
break;
}
return angle;
}
/**
* Convert an angle in the CbCr plane to values cb,cr in the YCbCr color space.
* @see <a href="https://xmpp.org/extensions/xep-0392.html#algorithm-cbcr">§5.3: CbCr generation</a>
*
* @param angle angel in CbCr plane.
* @return value pair cb,cr
*/
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};
}
/**
* 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 cbcr value pair from the YCbCr color space
* @return RGB value triple (R,G,B in [0,1])
*/
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};
}
/**
* 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;
}
/**
* Treat a signed java byte as unsigned to get its numerical value.
*
* @param b signed java byte
* @return integer value of its unsigned representation
*/
private static int u(byte b) {
// Get unsigned value of signed byte as an integer.
return b & 0xFF;
}
/**
* Return the consistent RGB color value of the input.
* This method uses the default {@link ConsistentColorSettings}.
*
* @param input input string (for example username)
* @return consistent color of that username as RGB values in range [0,1].
* @see #RGBFrom(CharSequence, ConsistentColorSettings)
*/
public static float[] RGBFrom(CharSequence input) {
return RGBFrom(input, DEFAULT_SETTINGS);
}
/**
* Return the consistent RGB color value for the input.
* This method respects the color vision deficiency mode set by the user.
*
* @param input input string (for example username)
* @param settings the settings for consistent color creation.
* @return consistent color of that username as RGB values in range [0,1].
*/
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;
}
public static class ConsistentColorSettings {
private final Deficiency deficiency;
public ConsistentColorSettings() {
this(Deficiency.none);
}
public ConsistentColorSettings(Deficiency deficiency) {
this.deficiency = Objects.requireNonNull(deficiency, "Deficiency must be given");
}
/**
* Return the deficiency setting.
*
* @return deficiency setting.
*/
public Deficiency getDeficiency() {
return deficiency;
}
}
}

View file

@ -0,0 +1,21 @@
/**
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Smack's API for XEP-0392: Consistent Color Generation.
*/
package org.jivesoftware.smackx.colors;

View file

@ -16,6 +16,6 @@
*/
/**
* Smack's API for XEP-352: Client State Indication.
* Smack's API for XEP-0352: Client State Indication.
*/
package org.jivesoftware.smackx.csi;

View file

@ -16,6 +16,6 @@
*/
/**
* Plain stream elements for XEP-352: Client State Indication.
* Plain stream elements for XEP-0352: Client State Indication.
*/
package org.jivesoftware.smackx.csi.packet;

View file

@ -16,6 +16,6 @@
*/
/**
* Provider for XEP-352: Client State Indication.
* Provider for XEP-0352: Client State Indication.
*/
package org.jivesoftware.smackx.csi.provider;

View file

@ -16,6 +16,6 @@
*/
/**
* Smack's API for XEP-332: HTTP over XMPP transport.
* Smack's API for XEP-0332: HTTP over XMPP transport.
*/
package org.jivesoftware.smackx.hoxt;

View file

@ -16,6 +16,6 @@
*/
/**
* Stanzas and extension elements for XEP-332: HTTP over XMPP transport.
* Stanzas and extension elements for XEP-0332: HTTP over XMPP transport.
*/
package org.jivesoftware.smackx.hoxt.packet;

View file

@ -16,6 +16,6 @@
*/
/**
* Providers for XEP-332: HTTP over XMPP transport.
* Providers for XEP-0332: HTTP over XMPP transport.
*/
package org.jivesoftware.smackx.hoxt.provider;

View file

@ -16,6 +16,6 @@
*/
/**
* Extension elements for XEP-295: JSON Encodings for XMPP.
* Extension elements for XEP-0295: JSON Encodings for XMPP.
*/
package org.jivesoftware.smackx.json.packet;

View file

@ -16,6 +16,6 @@
*/
/**
* Providers for XEP-295: JSON Encodings for XMPP.
* Providers for XEP-0295: JSON Encodings for XMPP.
*/
package org.jivesoftware.smackx.json.provider;

View file

@ -0,0 +1,152 @@
/**
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.colors;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.colors.ConsistentColor.Deficiency;
import org.junit.Test;
public class ConsistentColorsTest extends SmackTestSuite {
// Margin of error we allow due to floating point arithmetic
private static final float EPS = 0.001f;
private static final ConsistentColor.ConsistentColorSettings noDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.none);
private static final ConsistentColor.ConsistentColorSettings redGreenDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.redGreenBlindness);
private static final ConsistentColor.ConsistentColorSettings blueBlindnessDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.blueBlindness);
/*
Below tests check the test vectors from XEP-0392 §13.2.
*/
@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);
}
@Test
public void romeoRedGreenBlindnessTest() {
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() {
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() {
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() {
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() {
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() {
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() {
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() {
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() {
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() {
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() {
String value = "council";
float[] expected = new float[] {0.732f, 0.904f, 0.000f};
float[] actual = ConsistentColor.RGBFrom(value, blueBlindnessDeficiency);
assertRGBEquals(expected, actual, EPS);
}
/**
* Check, whether the values of two float arrays of size 3 are pairwise equal with an allowed error of eps.
*
* @param expected expected values
* @param actual actual values
* @param eps allowed error
*/
private static void assertRGBEquals(float[] expected, float[] actual, float eps) {
assertEquals(3, expected.length);
assertEquals(3, actual.length);
for (int i = 0; i < actual.length; i++) {
assertTrue(Math.abs(expected[i] - actual[i]) < eps);
}
}
}