Converting HSB to RGB using UIColor

I am working on building a color wheel picker UI for an iOS app. While there are several great Color Wheels that the developer community has written, I am actually trying to build a Color Wheel from scratch to understand how these color wheels and UIs are made. One of the first things is figuring out how to convert HSB values to RGB. I made a quick Swift Playground to get started:


// ColorExperiments Playground

import UIKit


// A HSB color where:
// H = 5 degrees out of 360 degrees
// S = 91% -> 0.91
// B = 100% -> 1.0

var hueColor = UIColor.init(hue: 5/360, saturation: 0.91, brightness: 1.0, alpha: 1.0)

// The RGB equivalent should be
// R = 255
// G = 42
// B = 23


I tried looking into the UIKit documentation and online, maybe I missed it, but is there an API in UIColor that allows me to convert the HSB to RGB color? I found some third party functions. I'm guessing UIColor should have something since the Swift Playgrounds sidebar shows me the my hueColor variable as the RGB equivalent unless that's some private function that Apple is using.


Thanks!

Replies

I have an app called ColorCeption.

It has a simple function to derive redValue, greenValue and blueValue

from hueValue, saturationValue, and brightnessValue

all of which are global floats from 0 to 1.0

and back again:


-(void)setRGBBasedOnHSB{
    
    float redH,greenH,blueH;
    float hue=hueValue*6;
    if(hue<=1.0){
        redH=1.0;greenH=hue;blueH=0.0;
    }else if (hue<2.0){
        redH=(2.0-hue);greenH=1.0;blueH=0.0;
    }else if (hue <3.0){
        redH=0.0;greenH=1.0;blueH=(hue-2.0);
    }else if (hue<4.0){
        redH=0.0;greenH=4.0-hue;blueH=1.0;
    }else if (hue<5.0){
        redH=hue-4.0;greenH=0.0;blueH=1.0;
    }else{
        redH=1.0;greenH=0.0;blueH=6.0-hue;
    }
    int color255Value;
    color255Value=255.*((1-saturationValue)*brightnessValue +saturationValue*brightnessValue*redH)+.1;
    redValue=color255Value/255.;
    color255Value=255.*((1-saturationValue)*brightnessValue +saturationValue*brightnessValue*greenH)+.1;
    greenValue=color255Value/255.;
    color255Value=255.*((1-saturationValue)*brightnessValue +saturationValue*brightnessValue*blueH)+.1;
    blueValue=color255Value/255.;
    
}






-(void)setHSBbasedOnRGB{
    float highColor=redValue;
    float mediumColor=greenValue;
    float lowColor=blueValue;
    float temp=highColor;
    if(highColor<mediumColor){
        highColor=mediumColor;
        mediumColor=temp;
    }
    if(mediumColor<lowColor){
        temp=mediumColor;
        mediumColor=lowColor;
        lowColor=temp;
    }
    if(highColor<mediumColor){
        temp=highColor;
        highColor=mediumColor;
        mediumColor=temp;
    }
    
    
    float hueShift=(mediumColor-lowColor)/(highColor-lowColor +.000001);
    float hue;
    if(redValue>=greenValue){
        if(redValue>=blueValue){
            if(greenValue>=blueValue){
                hue=hueShift;
                if(greenValue==blueValue && redValue==blueValue){
                    hue=hueValue*6.0; // white or black, don't change hueValue
          //          NSLog(@"hue value being preserved");
                }
            }else{
                hue=6.0-hueShift;
            }
        }else{
            hue=4.0+hueShift;
        }
    }else if(redValue>=blueValue){
        hue=2.0-hueShift;
    }else if(greenValue>=blueValue ){
        hue=2.0+hueShift;
    }else{
        hue=4.0-hueShift;
    }
    
    hueValue = hue/6.0;
    if(highColor>0.0)saturationValue=1-lowColor/highColor;  // preserve saturation if color is black
    brightnessValue=highColor;
    
}