hsv java_rgb-hsv-hsl-android.java
rgb-hsv-hsl-android.java
public class ColorUtility{
//RGB->HSL算法來源:http://blog.csdn.net/idfaya/article/details/6770414
static float[] colorToHsl(int color) {
float h = 0, s = 0, l = 0;
float r = Color.red(color) / 255f;
float g = Color.green(color) / 255f;
float b = Color.blue(color) / 255f;
float max = Math.max(Math.max(r,g), b);
float min = Math.min(Math.min(r,g), b);
if(max == min)
h = 0;
else if((max == r) && (g >= b))
h = 60 * (g - b) / (max - min);
else if((max == r) && (g < b))
h = 60 * (g - b) / (max - min) + 360;
else if(max == g)
h = 60 * (b - r) / (max - min) + 120;
else if(max == b)
h = 60 * (r - g) / (max - min) + 240;
l = (max + min) / 2;
if((l == 0) || (max == min))
s = 0;
else if((l > 0) && (l <= 1/2))
s = (max - min) / (max + min);
else if(l > 1/2)
s = (max - min) / (2 - (max+min));
return new float[]{h, s, l};
}
public static void setColor(int color) {
float hsv[] = new float[3];
Color.colorToHSV(color, hsv);
float hsl[] = colorToHsl(color);
Log.i("light", String.format("r,g,b = %3d,%3d,%3d; h,s,v = %.2f,%.2f,%.2f; h,s,l = %.2f,%.2f,%.2f", Color.red(color), Color.green(color), Color.blue(color), hsv[0], hsv[1], hsv[2], hsl[0], hsl[1], hsl[2]));
}
}
總結
以上是生活随笔為你收集整理的hsv java_rgb-hsv-hsl-android.java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java json.pasent_来自C
- 下一篇: php fpm 三个模式_php-fpm