有没有什么办法在android studio中将RGB值(0-255)转换为CMYK值(0-99)?
问题描述:
我正在制作一个应用程序来控制5050 LED灯条的LED颜色。颜色选择器为我提供RGB代码,但5050 LED灯带遵循CMYK颜色格式。有什么办法在android studio中将RGB值(0-255)转换为CMYK值(0-99)?有没有什么办法在android studio中将RGB值(0-255)转换为CMYK值(0-99)?
答
这是一个简单的类,将RGB转换为CMYK,正常的cmyk是从0.0到1.0,只需使用一个缩放器即可获得0到99.没有找到本机转换为cmyk的android颜色转换器。
public class CMYK
{
float cyan = 0.0f;
float magenta = 0.0f;
float yellow = 0.0f;
float black = 0.0f;
public void convertRGBtoCMYK(int r, int g, int b)
{
float _r = (float) (r/255);
float _g = (float) (g/255);
float _b = (float) (b/255);
black = 1.0f - max(_r, _g, _b);
cyan = (1.0f - _r - black)/(1.0f - black);
magenta = (1.0f - _g - black)/(1.0f - black);
yellow = (1.0f - _b - black)/(1.0f - black);
}
private float max(float a, float b, float c)
{
if (a > b && a > c)
return a;
if (b > a && b > c)
return b;
if (c > a && c > b)
return c;
// all equal just return a
return a;
}
}