将RGB值转换为HSV [英] Convert RGB value to HSV
本文介绍了将RGB值转换为HSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在互联网上找到了一种将RGB值转换为HSV值的方法。
不幸的是,当值为R = G = B时,我得到一个NaN,因为0/0操作。
I've found a method on the Internet to convert RGB values to HSV values. Unfortunately, when the values are R=G=B, I'm getting a NaN, because of the 0/0 operation.
你知道吗?在Java中有一个实现这种转换的方法,或者当我得到0/0分区以获得正确的HSV值时我该怎么做?
Do you know if there is an implemented method for this conversion in Java, or what do I have to do when I get the 0/0 division to get the right value of the HSV?
这是我的方法,改编自互联网上的一些代码:
Here comes my method, adapted from some code on the Internet:
public static double[] RGBtoHSV(double r, double g, double b){
double h, s, v;
double min, max, delta;
min = Math.min(Math.min(r, g), b);
max = Math.max(Math.max(r, g), b);
// V
v = max;
delta = max - min;
// S
if( max != 0 )
s = delta / max;
else {
s = 0;
h = -1;
return new double[]{h,s,v};
}
// H
if( r == max )
h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
h = 2 + ( b - r ) / delta; // between cyan & yellow
else
h = 4 + ( r - g ) / delta; // between magenta & cyan
h *= 60; // degrees
if( h < 0 )
h += 360;
return new double[]{h,s,v};
}
推荐答案
我很确定是什么你想要的是 RGBtoHSB
I'm pretty sure what you want is RGBtoHSB
int r = ...
int g = ...
int b = ...
float[] hsv = new float[3];
Color.RGBtoHSB(r,g,b,hsv)
//hsv contains the desired values
这篇关于将RGB值转换为HSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文