add color saturation utility

This commit is contained in:
Hannes Janetzek 2013-02-23 16:39:27 +01:00
parent d91ad535a2
commit 3e42886097
2 changed files with 39 additions and 7 deletions

View File

@ -54,31 +54,40 @@ public class FastMath {
return (pow > 0 ? (1 << pow) : (1.0f / (1 << -pow)));
}
public static int clamp(int value, int max, int min){
public static int clamp(int value, int max, int min) {
return (value < min ? min : (value > max ? max : value));
}
public static byte clampToByte(int value){
return (byte)(value < 0 ? 0 : (value > 255 ? 255 : value));
public static float clamp(float value, float max, float min) {
return (value < min ? min : (value > max ? max : value));
}
public static float abs(float value){
public static float clampN(float value) {
return (value < 0f ? 0f : (value > 1f ? 1f : value));
}
public static byte clampToByte(int value) {
return (byte) (value < 0 ? 0 : (value > 255 ? 255 : value));
}
public static float abs(float value) {
return value < 0 ? -value : value;
}
public static float absMax(float value1, float value2){
public static float absMax(float value1, float value2) {
float a1 = value1 < 0 ? -value1 : value1;
float a2 = value2 < 0 ? -value2 : value2;
return a2 < a1 ? a1 : a2;
}
// test if any absolute value is greater than 'cmp'
public static boolean absMaxCmp(float value1, float value2, float cmp){
public static boolean absMaxCmp(float value1, float value2, float cmp) {
return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp;
}
// test if any absolute value is greater than 'cmp'
public static boolean absMaxCmp(int value1, int value2, int cmp){
public static boolean absMaxCmp(int value1, int value2, int cmp) {
return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp;
}
}

View File

@ -257,6 +257,29 @@ public class GlUtils {
return c;
}
/**
* public-domain function by Darel Rex Finley
* from http://alienryderflex.com/saturation.html
*
* @param color
* The passed-in RGB values can be on any desired scale, such as
* 0 to 1, or 0 to 255.
* @param change
* 0.0 creates a black-and-white image.
* 0.5 reduces the color saturation by half.
* 1.0 causes no change.
* 2.0 doubles the color saturation.
*/
public static void changeSaturation(float color[], float change) {
float r = color[0];
float g = color[1];
float b = color[2];
double p = Math.sqrt(r * r * 0.299f + g * g * 0.587f + b * b * 0.114f);
color[0] = FastMath.clampN((float) (p + (r - p) * change));
color[1] = FastMath.clampN((float) (p + (g - p) * change));
color[2] = FastMath.clampN((float) (p + (b - p) * change));
}
private final static float[] mIdentity = {
1, 0, 0, 0,
0, 1, 0, 0,