UIColor not working with RGBA values
RGB values for UIColor are between 0 and 1 (see the documentation "specified as a value from 0.0 to 1.0")
You need to divide your numbers by 255:
passwordTextField.textColor = UIColor(red: CGFloat(202.0/255.0), green: CGFloat(228.0/255.0), blue: CGFloat(230.0/255.0), alpha: CGFloat(1.0))
Another thing, you don't need to create CGFloats:
passwordTextField.textColor = UIColor(red:202.0/255.0, green:228.0/255.0, blue:230.0/255.0, alpha:1.0)
UIColor(r: g: b:) not working properly
The RGB values range from 0.0 to 1.0, not 0 to 255.
Why does only the built-in UIColors work here?
This should work for you:
func switchColor(data: UInt32) {
guard let contents = backgroundGeometry.firstMaterial?.diffuse.contents else {
fatalError("First material is nil") // If this really can be empty, just replace this with return
}
switch data {
case 1..<200:
contents = UIColor(red: 242/255, green: 90/255, blue: 90/255, alpha: 1)
case 200..<400:
contents = UIColor(red: 252/255, green: 162/255, blue: 115/255, alpha: 1)
case 400..<600:
contents = UIColor(red: 244/255, green: 235/255, blue: 99/255, alpha: 1)
case 600..<800:
contents = UIColor(red: 110/255, green: 195/255, blue: 175/255, alpha: 1)
case 800..<1000:
contents = UIColor(red: 91/255, green: 118/255, blue: 211/255, alpha: 1)
default:
contents = .green
}
}
The maximum value of a color is 1.0, not 255. Therefore you need to divide the values.
How convert RGBA to UIColor and back
Welcome!
The issue is that you use two different color spaces:
- When converting from
RGBA
toUIColor
, you assume the RGBA values are in Display P3 (UIColor(displayP3Red:...
). - But the
CGContext
you create with the "device RGB" color space, which is an sRGB color space as far as I know.
If you'd use the init(red:, green:, blue:, alpha:)
initializer of UIColor
it should work.
How do I create a UIColor from RGBA?
Your values are incorrect, you need to divide each color value by 255.0.
[UIColor colorWithRed:66.0f/255.0f
green:79.0f/255.0f
blue:91.0f/255.0f
alpha:1.0f];
The docs state:
+ (UIColor *)colorWithRed:(CGFloat)red
green:(CGFloat)green
blue:(CGFloat)blue
alpha:(CGFloat)alpha
Parameters
red
The red component of the color object, specified as a value from 0.0 to 1.0.
green
The green component of the color object, specified as a value from 0.0 to 1.0.
blue
The blue component of the color object, specified as a value from 0.0 to 1.0.
alpha
The opacity value of the color object, specified as a value from 0.0 to 1.0.
Reference here.
Objective-c HTML-RGB color not correct in UIColor
Simply divide all you color values by 255:
[UIColor colorWithRed:235/255.0f green:242/255.0f blue:212/255.0f alpha:1]
UIColor doesn't work with it's own properties
I believe your problem is that grayColor
is not defined with a colorspace that's compatible with the getRed:green:blue:alpha:
method. You can verify this by initializing the rgba
values to something impossible for a color and noting that they are not changed by the call, which matches the documented return behavior. Or, replace grayColor
with something that explicitly includes colors, such as cyanColor
and the getRed:green:blue:alpha:
method works.
Here's a bit of code to demonstrate:
float r = 5.0,g = 6.0,b = 7.0,a = 8.0, w = 3.0, a2 = 4.0;
UIColor *aaa = [UIColor grayColor];
[aaa getRed:&r green:&g blue:&b alpha:&a];
[aaa getWhite:&w alpha:&a2];
(On OS X there are methods for converting between colorspaces but I'm not seeing iOS equivalents.)
Swift - Custom color not applying to barTintColor
You need to give color by dividing them by 255
, the syntax is like this.
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
Each of the ones that take a CGFloat
take a value between 0.0 and 1.0, referring to either the complete absence of, or the maximum amount of that color component respectively. So, this means that even if you have pure RGB
values in decimal or hexadecimal format, you will have to divide them by decimal 255 to get the amount to input here.
let customRedColor = UIColor(red: 255/255.0 , green: 0, blue: 13/255.0, alpha: 1.0)
//or Direct
let customRedColor = UIColor(red: 1.0 , green: 0, blue: 0.05, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = customRedColor
UIColor Components
The values aren't zero, they're just printing that way since you're printing a CGFloat
as a Float
. To fix it:
NSLog("%f, %f, %f, %f", Float(red1), Float(green1), Float(blue1), Float(alpha1))
or
NSLog("%@, %@, %@, %@", red1, green1, blue1, alpha1)
or
println("\(red1), \(green1), \(blue1), \(alpha1)")
Related Topics
How to Create a Multiline Textfield in Swiftui
How to Use Timer (Formerly Nstimer) in Swift
Background Location Services Not Working in iOS 7
How to Use Background Thread in Swift
How to Record Audio on Iphone With Avaudiorecorder
How to Install iOS 6 Sdk on Xcode 5
Save String to the Nsuserdefaults
How to Parse a Json File in Swift
Eliminate Extra Separators Below Uitableview
Move Textfield When Keyboard Appears Swift
How to Perform Unwind Segue Programmatically
How to Get My Ip Address Programmatically on Ios/Macos
Detecting Taps on Attributed Text in a Uitextview in Ios
What Does Enable_Bitcode Do in Xcode 7
Load Resources from Relative Path Using Local HTML in Uiwebview