206 lines
6.0 KiB
Java
206 lines
6.0 KiB
Java
/**
|
|
*
|
|
* 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.SHA1;
|
|
|
|
public class ConsistentColor {
|
|
|
|
// 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 {
|
|
none,
|
|
redGreenBlindness,
|
|
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) {
|
|
double _angle = angle;
|
|
switch (deficiency) {
|
|
case none:
|
|
break;
|
|
case redGreenBlindness:
|
|
_angle = _angle % Math.PI;
|
|
break;
|
|
case blueBlindness:
|
|
_angle -= Math.PI / 2;
|
|
_angle = _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 for the input.
|
|
* This method respects the color vision deficiency mode set by the user.
|
|
*
|
|
* @param input input string (for example username)
|
|
* @return consistent color of that username as RGB values in range [0,1].
|
|
*/
|
|
public static float[] RGBFrom(CharSequence input, Context context) {
|
|
double angle = createAngle(input);
|
|
double correctedAngle = applyColorDeficiencyCorrection(angle, context.getDeficiency());
|
|
double[] CbCr = angleToCbCr(correctedAngle);
|
|
float[] rgb = CbCrToRGB(CbCr, Y);
|
|
return rgb;
|
|
}
|
|
|
|
public static class Context {
|
|
|
|
private Deficiency deficiency = Deficiency.none;
|
|
|
|
/**
|
|
* Activate color correction for users suffering from red-green-blindness.
|
|
*/
|
|
public void activateRedGreenBlindnessCorrection() {
|
|
deficiency = Deficiency.redGreenBlindness;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Return the deficiency setting.
|
|
*
|
|
* @return deficiency setting.
|
|
*/
|
|
public Deficiency getDeficiency() {
|
|
return deficiency;
|
|
}
|
|
}
|
|
}
|