My ques may be silly. Is there an hexa code for [UIColor clearColor]? If so, what is the code? Thanks in advance.
RGBA Hex Code for ClearColor:
NNNNNN00 whereas N may be any hex-value (0-F).
UIColor *clearColor = [UIColor clearColor];
CGFloat red = 0;
CGFloat green = 0;
CGFloat blue = 0;
CGFloat alpha = 0;
[clearColor getRed:&red green:&green blue:&blue alpha:&alpha];
NSLog(#"red: %.3f, green: %.3f, blue: %.3f, alpha: %.3f",
red, green, blue, alpha);
NSLog(#"red: 0x%02x, green: 0x%02x, blue: 0x%02x, alpha: 0x%02x",
(int)(red*255.0), (int)(green*255.0), (int)(blue*255.0), (int)(alpha*255.0));
NSLog output:
red: 0.000, green: 0.000, blue: 0.000, alpha: 0.000
red: 0x00, green: 0x00, blue: 0x00, alpha: 0x00
As far as I know, hexadecimal color codes are made up of RRGGBB values, whereas [UIColor clearColor] depends very much on there being an alpha component.
So the answer is "nope", there's no hexa code for clearColor.
I like using hex colours in my configuration, so I modified a commonly used macro to split RGBA hex (eg. #00000055 semi-transparent black) into a UIColor with alpha:
#define UIColorFromRGBA(rgbaValue) [UIColor \
colorWithRed:((float)((rgbaValue & 0xFF000000) >> 24))/255.0 \
green:((float)((rgbaValue & 0xFF0000) >> 16))/255.0 \
blue:((float)((rgbaValue & 0xFF00) >> 8))/255.0 \
alpha:((float)(rgbaValue & 0xFF))/255.0 ]
Example usage:
cell.backgroundColor = UIColorFromRGBA(0x00000055);
Related
I get HSB as 29, 90, 100.
How to convert it to the range 0...1?
UIColor is initialized only in this range, so I have a question.
let red: CGFloat = 1
let green: CGFloat = 0.5372
let blue: CGFloat = 0.0941
let hueOut = 29
let satOut = 90
let brightnessOut = 100
let color = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
///r 1,0 g 0,537 b 0,094 a 1,0
let color2 = UIColor(hue: hueOut, saturation: satOut, brightness: brightnessOut, alpha: 1.0)
///r -9,0 g 0,0 b -3,0 a 1,0
Looks like your hue range is 0...360 and your saturation and brightness are 0...100. You just need to convert your integer to double and divide by 360 or 100:
let color2 = UIColor(hue: Double(hueOut)/360, saturation: Double(satOut)/100, brightness: Double(brightnessOut)/100, alpha: 1.0)
This will result in r 1.0 g 0.535 b 0.1 a 1.0
I have an array of colours that look like this...
var purpleShades: [(CGFloat, CGFloat, CGFloat)] = [(186.0/255.0, 85.0/255.0, 211.0/255.0), (147.0/255.0, 112.0/255.0, 219.0/255.0), (138.0/255.0, 43.0/255.0, 226.0/255.0), (148.0/255.0, 0.0/255.0, 211.0/255.0), (153.0/255.0, 50.0/255.0, 204.0/255.0), (139.0/255.0, 0.0/255.0, 139.0/255.0)]
rather than duplicate code was wondering if anyone could help convert it to UIColor, so I can use it for this piece of code.
cell.tintColor = grayShades[Int(index)]
This variation of init might help you
It accepts red, green, blue and alpha as parameters.
Here's a nice extension to UIColor:
extension UIColor {
convenience init(hex: UInt, alpha: CGFloat) {
var red, green, blue: UInt
red = ((hex & 0xFF0000) >> 16)
green = ((hex & 0x00FF00) >> 8)
blue = hex & 0x0000FF
self.init(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: alpha)
}
}
With that you can write:
let purple = UIColor(hex: 0x9932CC, alpha: 1)
If you have a lot of colours, another extension on UIColor gives you…
extension UIColor {
static let darkOrchid = UIColor(hex: 0x 9932CC, alpha: 1)
static let darkMagenta = UIColor(hex: 0x 8B008B, alpha: 1)
static let indigo = UIColor(hex: 0x 4B0082, alpha: 1)
}
which allows you to say, for example…
cell.tintColor = .darkOrchid
This question already has answers here:
How to get the RGB Code (INT) from an UIColor in Swift [duplicate]
(7 answers)
Closed 6 years ago.
I got this UIColor :
UIColor(red: 0.2, green: 0.4118, blue: 0.1176, alpha: 1.0)
And I need to convert in Uint. How can I do that?
EDIT :
func showEmailMessage(advice : String)
{
_ = SCLAlertView().showSuccess("Congratulation", subTitle: advice, closeButtonTitle: "Ok", duration : 10, colorStyle: 0x33691e, colorTextButton: 0xFFFFFF)
}
Color style field want Uint
You can make use of the UIColor.getRed(...) method to extract the colors as CGFloat, thereafter convert the values of your CGFloat triplet to the proper bit positions of an UInt32 variable.
// Example: use color triplet CC6699 "=" {204, 102, 153} (RGB triplet)
let color = UIColor(red: 204.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1.0)
// read colors to CGFloats and convert and position to proper bit positions in UInt32
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
if color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
var colorAsUInt : UInt32 = 0
colorAsUInt += UInt32(red * 255.0) << 16 +
UInt32(green * 255.0) << 8 +
UInt32(blue * 255.0)
colorAsUInt == 0xCC6699 // true
}
For details, see e.g. the Language Guide - Advanced Operators which contains, among other valuable things, an example specifically for bit shifting w.r.t RGB triplets.
I'm doing the following in order to retrieve the hue from a UIColor():
let rgbColour = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
var hue: CGFloat = 0
var saturation: CGFloat = 0
var brightness: CGFloat = 0
var alpha: CGFloat = 0
rgbColour.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
println("\(hue),\(saturation),\(brightness)")
Output:
1.0,1.0,1.0
According to this link, I'm meant to be getting 0.0,1.0,1.0 for RGB (red) 1.0,0.0,0.0.
Am I doing something wrong?
First of all the range of the red/green/blue components in UIColor is 0.0 .. 1.0,
not 0.0 .. 255.0, so you probably want
let rgbColour = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
But even then you get the output 1.0,1.0,1.0 and this is correct.
The hue component ranges from 0.0 to 1.0, which corresponds to the angle from 0º to 360º
in a color wheel (see for example HSL and HSV).
Therefore hue = 0.0 and hue = 1.0 describe an identical color.
If you need to normalize the hue component to the half-open interval
0.0 <= hue < 1.0 then you could do that with
hue = fmod(hue, 1.0)
To build on #Martin R's answer:
If you wanted to use HSB, you need to:
Divide the hue value by 360
Use decimals for the Saturation and Brightness values
So, say for example that Sketch is telling you the colour values in HSB are: Hue: 20, Saturation: 72 and Brightness: 96
In Xcode, create the colour as follows:
let myAwesomeColour = UIColor(hue: 20/360, saturation: 0.72, brightness: 0.96, alpha: 1.0)
Whether you use RGB or HSB is a matter of preference. The results are the same as far as Xcode is concerned, they both translate to a UIColor.
how to display image view background color with hash values.
i need to place image view background color with #028002.
can any one please help me,
Thank u in advance.
I've used next UIColor Category:
#implementation UIColor(Tools)
+ (UIColor *)colorWithHex:(NSInteger)hex alpha:(CGFloat)alpha {
return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:alpha];
}
#end
// Example of Usage:
// backgroundView.backgroundColor = [UIColor colorWithHex:0xe1e1e1 alpha:1.0];
There is no need to parse strings here.
UIColor does not support hex value directly. You need to convert that yourself. Something like this:
CGFloat r = (CGFloat)0x02 / 0xFF;
CGFloat g = (CGFloat)0x80 / 0xFF;
CGFloat b = (CGFloat)0x02 / 0xFF;
[UIColor colorWithRed:r green:g blue:b alpha:1.0]
If you want to convert hash value to UIColor this may help you
http://imthi.com/blog/programming/iphone-sdk-convert-hex-color-string-to-uicolor.php
In swift you can use below method
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}