1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 00:02:06 +01:00

Improve ConsistentColor API

- Rename Context to ConsistentColorSettings
- Add convenience RGBFrom(CharSequence) method
- Remove _angle in code

SMACK-800.
This commit is contained in:
Florian Schmaus 2018-02-10 21:03:17 +01:00
parent e0af3db189
commit e38f80b3cb
2 changed files with 45 additions and 48 deletions

View file

@ -16,10 +16,13 @@
*/
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;
@ -29,8 +32,19 @@ public class ConsistentColor {
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
}
@ -57,20 +71,19 @@ public class ConsistentColor {
* @return corrected angle in CbCr plane
*/
private static double applyColorDeficiencyCorrection(double angle, Deficiency deficiency) {
double _angle = angle;
switch (deficiency) {
case none:
break;
case redGreenBlindness:
_angle = _angle % Math.PI;
angle %= Math.PI;
break;
case blueBlindness:
_angle -= Math.PI / 2;
_angle = _angle % Math.PI;
_angle += Math.PI / 2;
angle -= Math.PI / 2;
angle %= Math.PI;
angle += Math.PI / 2;
break;
}
return _angle;
return angle;
}
/**
@ -153,44 +166,44 @@ public class ConsistentColor {
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, Context context) {
public static float[] RGBFrom(CharSequence input, ConsistentColorSettings settings) {
double angle = createAngle(input);
double correctedAngle = applyColorDeficiencyCorrection(angle, context.getDeficiency());
double correctedAngle = applyColorDeficiencyCorrection(angle, settings.getDeficiency());
double[] CbCr = angleToCbCr(correctedAngle);
float[] rgb = CbCrToRGB(CbCr, Y);
return rgb;
}
public static class Context {
public static class ConsistentColorSettings {
private Deficiency deficiency = Deficiency.none;
private final Deficiency deficiency;
/**
* Activate color correction for users suffering from red-green-blindness.
*/
public void activateRedGreenBlindnessCorrection() {
deficiency = Deficiency.redGreenBlindness;
public ConsistentColorSettings() {
this(Deficiency.none);
}
/**
* Activate color correction for users suffering from blue-blindness.
*/
public void activateBlueBlindnessCorrection() {
deficiency = Deficiency.blueBlindness;
}
/**
* Deactivate color vision deficiency correction.
*/
public void deactivateDeficiencyCorrection() {
deficiency = Deficiency.none;
public ConsistentColorSettings(Deficiency deficiency) {
this.deficiency = Objects.requireNonNull(deficiency, "Deficiency must be given");
}
/**

View file

@ -21,6 +21,8 @@ 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 {
@ -28,15 +30,9 @@ 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.Context noDeficiency = new ConsistentColor.Context();
private static final ConsistentColor.Context redGreenDeficiency = new ConsistentColor.Context();
private static final ConsistentColor.Context blueBlindnessDeficiency = new ConsistentColor.Context();
public ConsistentColorsTest() {
noDeficiency.deactivateDeficiencyCorrection();
redGreenDeficiency.activateRedGreenBlindnessCorrection();
blueBlindnessDeficiency.activateBlueBlindnessCorrection();
}
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.
@ -138,18 +134,6 @@ public class ConsistentColorsTest extends SmackTestSuite {
assertRGBEquals(expected, actual, EPS);
}
@Test
public void contextGetterTest() {
ConsistentColor.Context context = new ConsistentColor.Context();
assertEquals(ConsistentColor.Deficiency.none, context.getDeficiency());
context.activateBlueBlindnessCorrection();
assertEquals(ConsistentColor.Deficiency.blueBlindness, context.getDeficiency());
context.activateRedGreenBlindnessCorrection();
assertEquals(ConsistentColor.Deficiency.redGreenBlindness, context.getDeficiency());
context.deactivateDeficiencyCorrection();
assertEquals(ConsistentColor.Deficiency.none, context.getDeficiency());
}
/**
* Check, whether the values of two float arrays of size 3 are pairwise equal with an allowed error of eps.
*