Is there function to convert UIColor to Hue Saturation Brightness?

前端 未结 4 1505
[愿得一人]
[愿得一人] 2020-12-05 05:08

i can set uicolor with RGB values:

[UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];

i can set uicolor with hsb values:

4条回答
  •  攒了一身酷
    2020-12-05 06:15

    Beware that it is a draft, but you can try this code, if you're lower to version 5.0

    ...
    CGFloat computedH = 0;
    CGFloat computedS = 0;
    CGFloat computedV = 0;
    
    CGFloat minRGB = MIN(r, MIN(g,b));
    CGFloat maxRGB = MAX(r, MAX(g,b));
    
    if (minRGB==maxRGB) {
       computedH = 0;
       computedS = 0;
       computedV = minRGB;
    } else {
       double d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
       double h = (r==minRGB) ? 3 : ((b==minRGB) ? 1 : 5);
       computedH = (60*(h - d/(maxRGB - minRGB))) / 360.;
       computedS = ((maxRGB - minRGB)/maxRGB);
       computedV = maxRGB;
    }
    ...
    

提交回复
热议问题