UIColor conversion from RGB to HSV , set brightness to UIColor

…衆ロ難τιáo~ 提交于 2019-12-06 05:22:00
realPro
+(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;
}

It's not clear to me if in your original question you really want "brightness" or alpha. RGB and HSV color schemes are completely different.

If I interpret your intent correctly:

You do not need to convert from RGB to HSV and back to RGB if all you want to do is control the alpha channel for an RGB color.

The simplest and most efficient way to achieve what you want to do is to simply add a slider for alpha and take the output from that to change color transparency.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!