I have a Color with HSB(Hue, Saturation, Brightness) values, and I want the convert RBG into a Hexadecimal string directly. How can I do that? Is there an approach without using the UIColor APIs, I'd like to implement my own function Like:
func toHex(h: CGFloat, s: CGFloat, b: CGFloat) -> String {
var hex: String
//conversion
return hex // For example "#02AAFE"
}
You can use UIColor initializer to create a UIColor, then extract the rgb colors using UIColor getRed method. Then you just need to multiply the components value by 255 and create an hexa representation from them using string radix initializer:
Now if you really want avoid using UIColor api you can use this post as a reference to convert from HSB to RGB: