Skip to content

Instantly share code, notes, and snippets.

@rock3r
Last active February 25, 2020 09:06
Show Gist options
  • Save rock3r/d026ae14cb98fad20bf6 to your computer and use it in GitHub Desktop.
Save rock3r/d026ae14cb98fad20bf6 to your computer and use it in GitHub Desktop.
ColorUtils: utilities for manipulating colours
package net.frakbot.util.color;
import android.graphics.Color;
/**
* The MIT License (MIT)
* <p/>
* Copyright (c) 2014 Sebastiano Poggi
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p/>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p/>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public class ColorUtils {
private static final int MASK_ALPHA = 0xFF000000;
private static final int MASK_RGB = 0x00FFFFFF;
private static final int ALPHA_SHIFT = 24;
private static final int COMPONENT_MIN = 0;
private static final int COMPONENT_MAX = 255;
private static final float COMPONENT_MIN_FLOAT = 0f;
private static final float COMPONENT_MAX_FLOAT = 1f;
private static final float COMPONENT_MIN_HUE = 0f;
private static final float COMPONENT_MAX_HUE = 360f;
private static final int HSV_HUE = 0;
private static final int HSV_SATURATION = 1;
private static final int HSV_VALUE = 2;
private static final ThreadLocal<float[]> FLOAT_CACHE;
static {
FLOAT_CACHE = new ThreadLocal<float[]>() {
@Override
protected float[] initialValue() {
return new float[3];
}
};
}
public static int getOpaqueVersionOf(int color) {
return MASK_ALPHA | extractRGB(color);
}
public static int setAlphaOf(int color, int alpha) {
alpha = clipComponent(alpha);
return (alpha << ALPHA_SHIFT) | extractRGB(color);
}
private static int extractRGB(int color) {
return color & MASK_RGB;
}
private static int clipComponent(int value) {
if (value < COMPONENT_MIN) {
return COMPONENT_MIN;
} else if (value > COMPONENT_MAX) {
return COMPONENT_MAX;
}
return value;
}
public static float hue(int originalColor) {
return getHSVComponent(originalColor, HSV_HUE);
}
public static float saturation(int originalColor) {
return getHSVComponent(originalColor, HSV_SATURATION);
}
public static float value(int originalColor) {
return getHSVComponent(originalColor, HSV_VALUE);
}
private static float getHSVComponent(int originalColor, int component) {
float[] hsv = FLOAT_CACHE.get();
Color.colorToHSV(originalColor, hsv);
return hsv[component];
}
public static int multiplyHueOf(int originalColor, float factor) {
float[] hsv = FLOAT_CACHE.get();
int alpha = Color.alpha(originalColor);
Color.colorToHSV(originalColor, hsv);
factor = Math.abs(factor);
hsv[HSV_HUE] = multiplyComponent(hsv[HSV_HUE], factor, COMPONENT_MAX_HUE);
return Color.HSVToColor(alpha, hsv);
}
public static int multiplySaturationOf(int originalColor, float factor) {
float[] hsv = FLOAT_CACHE.get();
int alpha = Color.alpha(originalColor);
Color.colorToHSV(originalColor, hsv);
factor = Math.abs(factor);
hsv[HSV_SATURATION] = multiplyComponent(hsv[HSV_SATURATION], factor, COMPONENT_MAX_FLOAT);
return Color.HSVToColor(alpha, hsv);
}
public static int multiplyValueOf(int originalColor, float factor) {
float[] hsv = FLOAT_CACHE.get();
int alpha = Color.alpha(originalColor);
Color.colorToHSV(originalColor, hsv);
factor = Math.abs(factor);
hsv[HSV_VALUE] = multiplyComponent(hsv[HSV_VALUE], factor, COMPONENT_MAX_FLOAT);
return Color.HSVToColor(alpha, hsv);
}
private static float multiplyComponent(float component, float factor, float componentMaxValue) {
return Math.min(component * factor, componentMaxValue);
}
public static int setHueOf(int originalColor, float hue) {
float[] hsv = FLOAT_CACHE.get();
int alpha = Color.alpha(originalColor);
Color.colorToHSV(originalColor, hsv);
hue = clipHueComponent(hue);
hsv[HSV_HUE] = hue;
return Color.HSVToColor(alpha, hsv);
}
public static int setSaturationOf(int originalColor, float saturation) {
float[] hsv = FLOAT_CACHE.get();
int alpha = Color.alpha(originalColor);
Color.colorToHSV(originalColor, hsv);
saturation = clipComponent(saturation);
hsv[HSV_SATURATION] = saturation;
return Color.HSVToColor(alpha, hsv);
}
public static int setValueOf(int originalColor, float value) {
float[] hsv = FLOAT_CACHE.get();
int alpha = Color.alpha(originalColor);
Color.colorToHSV(originalColor, hsv);
value = clipComponent(value);
hsv[HSV_VALUE] = value;
return Color.HSVToColor(alpha, hsv);
}
private static float clipComponent(float value) {
if (value < COMPONENT_MIN_FLOAT) {
return COMPONENT_MIN_FLOAT;
} else if (value > COMPONENT_MAX_FLOAT) {
return COMPONENT_MAX_FLOAT;
}
return value;
}
private static float clipHueComponent(float value) {
if (value < COMPONENT_MIN_HUE) {
return COMPONENT_MIN_HUE;
} else if (value > COMPONENT_MAX_HUE) {
return COMPONENT_MAX_HUE;
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment