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; package org.jivesoftware.smackx.colors;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.SHA1; import org.jivesoftware.smack.util.SHA1;
public class ConsistentColor { public class ConsistentColor {
private static final ConsistentColorSettings DEFAULT_SETTINGS = new ConsistentColorSettings();
// See XEP-0392 §13.1 Constants for YCbCr (BT.601) // See XEP-0392 §13.1 Constants for YCbCr (BT.601)
private static final double KR = 0.299; private static final double KR = 0.299;
private static final double KG = 0.587; private static final double KG = 0.587;
@ -29,8 +32,19 @@ public class ConsistentColor {
private static final double Y = 0.732; private static final double Y = 0.732;
public enum Deficiency { public enum Deficiency {
/**
* Do not apply measurements for color vision deficiency correction.
*/
none, none,
/**
* Activate color correction for users suffering from red-green-blindness.
*/
redGreenBlindness, redGreenBlindness,
/**
* Activate color correction for users suffering from blue-blindness.
*/
blueBlindness blueBlindness
} }
@ -57,20 +71,19 @@ public class ConsistentColor {
* @return corrected angle in CbCr plane * @return corrected angle in CbCr plane
*/ */
private static double applyColorDeficiencyCorrection(double angle, Deficiency deficiency) { private static double applyColorDeficiencyCorrection(double angle, Deficiency deficiency) {
double _angle = angle;
switch (deficiency) { switch (deficiency) {
case none: case none:
break; break;
case redGreenBlindness: case redGreenBlindness:
_angle = _angle % Math.PI; angle %= Math.PI;
break; break;
case blueBlindness: case blueBlindness:
_angle -= Math.PI / 2; angle -= Math.PI / 2;
_angle = _angle % Math.PI; angle %= Math.PI;
_angle += Math.PI / 2; angle += Math.PI / 2;
break; break;
} }
return _angle; return angle;
} }
/** /**
@ -153,44 +166,44 @@ public class ConsistentColor {
return b & 0xFF; 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. * Return the consistent RGB color value for the input.
* This method respects the color vision deficiency mode set by the user. * This method respects the color vision deficiency mode set by the user.
* *
* @param input input string (for example username) * @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]. * @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 angle = createAngle(input);
double correctedAngle = applyColorDeficiencyCorrection(angle, context.getDeficiency()); double correctedAngle = applyColorDeficiencyCorrection(angle, settings.getDeficiency());
double[] CbCr = angleToCbCr(correctedAngle); double[] CbCr = angleToCbCr(correctedAngle);
float[] rgb = CbCrToRGB(CbCr, Y); float[] rgb = CbCrToRGB(CbCr, Y);
return rgb; return rgb;
} }
public static class Context { public static class ConsistentColorSettings {
private Deficiency deficiency = Deficiency.none; private final Deficiency deficiency;
/** public ConsistentColorSettings() {
* Activate color correction for users suffering from red-green-blindness. this(Deficiency.none);
*/
public void activateRedGreenBlindnessCorrection() {
deficiency = Deficiency.redGreenBlindness;
} }
/** public ConsistentColorSettings(Deficiency deficiency) {
* Activate color correction for users suffering from blue-blindness. this.deficiency = Objects.requireNonNull(deficiency, "Deficiency must be given");
*/
public void activateBlueBlindnessCorrection() {
deficiency = Deficiency.blueBlindness;
}
/**
* Deactivate color vision deficiency correction.
*/
public void deactivateDeficiencyCorrection() {
deficiency = Deficiency.none;
} }
/** /**

View File

@ -21,6 +21,8 @@ import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.colors.ConsistentColor.Deficiency;
import org.junit.Test; import org.junit.Test;
public class ConsistentColorsTest extends SmackTestSuite { public class ConsistentColorsTest extends SmackTestSuite {
@ -28,15 +30,9 @@ public class ConsistentColorsTest extends SmackTestSuite {
// Margin of error we allow due to floating point arithmetic // Margin of error we allow due to floating point arithmetic
private static final float EPS = 0.001f; private static final float EPS = 0.001f;
private static final ConsistentColor.Context noDeficiency = new ConsistentColor.Context(); private static final ConsistentColor.ConsistentColorSettings noDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.none);
private static final ConsistentColor.Context redGreenDeficiency = new ConsistentColor.Context(); private static final ConsistentColor.ConsistentColorSettings redGreenDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.redGreenBlindness);
private static final ConsistentColor.Context blueBlindnessDeficiency = new ConsistentColor.Context(); private static final ConsistentColor.ConsistentColorSettings blueBlindnessDeficiency = new ConsistentColor.ConsistentColorSettings(Deficiency.blueBlindness);
public ConsistentColorsTest() {
noDeficiency.deactivateDeficiencyCorrection();
redGreenDeficiency.activateRedGreenBlindnessCorrection();
blueBlindnessDeficiency.activateBlueBlindnessCorrection();
}
/* /*
Below tests check the test vectors from XEP-0392 §13.2. Below tests check the test vectors from XEP-0392 §13.2.
@ -138,18 +134,6 @@ public class ConsistentColorsTest extends SmackTestSuite {
assertRGBEquals(expected, actual, EPS); 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. * Check, whether the values of two float arrays of size 3 are pairwise equal with an allowed error of eps.
* *