从RGB到HSV的UIColor转换,将亮度设置为UIColor [英] UIColor conversion from RGB to HSV , set brightness to UIColor
问题描述
在我的应用程序中,我试图用不同的颜色绘制。我有自己的颜色选择器,但我想添加到此选择器滑块以获得颜色亮度。对于底层测试:我选择在视图中作为背景颜色显示的颜色,并且我想要添加滑块,该滑块将从1滑动到0并更改颜色的亮度并在窗口中显示颜色。因此,我想询问是否有任何可能的转换方法。
In my app I am trying to draw with different colors. I have my own color picker, but I want to add to this picker slider for color brightness. For understudying : I choose a color which is displayed somewhere in view as background color and to this color I want add slider which will slide from 1 to 0 and change brightness of the color and displays the color in window. Therefore I want to ask if there is any possible way how to convert.
在我的项目中我使用它:
in my project I use this :
CGContextSetRGBStrokeColor(current, R, G, B, A);
所以当我滑动滑块时我需要更改颜色的亮度,因此我需要转换我的 UIColor
- 表示为RGB到HSV,然后再返回RGB再次使用它。
so anytime I slide the slider I need to change the brightness of the color, therefore I need to convert my UIColor
- represented as RGB to HSV and than back to RGB for using it one more time.
是否存在任何RGB到HSV转换算法,或者在iOS 5或Xcode4中是否存在?
Does exist any RGB to HSV conversion algorithm or is there any in iOS 5 or Xcode4?
推荐答案
+(struct hsv_color)HSVfromRGB:(struct rgb_color)rgb
{
struct hsv_color hsv;
CGFloat rgb_min, rgb_max;
rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
rgb_max = MAX3(rgb.r, rgb.g, rgb.b);
if (rgb_max == rgb_min) {
hsv.hue = 0;
} else if (rgb_max == rgb.r) {
hsv.hue = 60.0f * ((rgb.g - rgb.b) / (rgb_max - rgb_min));
hsv.hue = fmodf(hsv.hue, 360.0f);
} else if (rgb_max == rgb.g) {
hsv.hue = 60.0f * ((rgb.b - rgb.r) / (rgb_max - rgb_min)) + 120.0f;
} else if (rgb_max == rgb.b) {
hsv.hue = 60.0f * ((rgb.r - rgb.g) / (rgb_max - rgb_min)) + 240.0f;
}
hsv.val = rgb_max;
if (rgb_max == 0) {
hsv.sat = 0;
} else {
hsv.sat = 1.0 - (rgb_min / rgb_max);
}
return hsv;
}
这篇关于从RGB到HSV的UIColor转换,将亮度设置为UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!