Uicolor Colorwithred:Green:Blue:Alpha: Always Returns White Unless One Argument Is 0

UIColor colorWithRed:green:blue:alpha: always returns white unless one argument is 0

The parameter aren't a values of 0 to 255 but a float between 0.0 and 1.0:

[UIColor colorWithRed:0.0f/255.0f green:155.0f/255.0f blue:218.0f/255.0f alpha:1.0f]; 

fill border rect with custom color

The arguments for custom RGB codes must be CGFloats.

UIColor* color = [UIColor colorWithRed:80.0/255.0 green:80.0/255.0 blue:80.0/255.0 alpha:1];

would be the correct way to specify your custom RGB color

background color not working as expected

The arguments for colorWithRed:green:blue:alpha need to be CGFloats on a scale from 0 to 1, where 0 is 0 and 1 is 255. Therefore, your code should look more like this:

UIColor *color = [UIColor colorWithRed:76.0/255.0
green:76.0/255.0
blue:76.0/255.0
alpha:1.0];

Swift border color

The input values needs to be between 0 and 1, so divide them by 255.

buyButton.layer.borderColor = UIColor(red: 83.0/255.0, green: 186.0/255.0, blue: 183.0/255.0, alpha: 1.0).cgColor

How do I get red green blue (RGB) and alpha back from a UIColor object?

The reason for the crash when accessing SelectedColor.CGColor could be that you do not retain the result from getColor, perhaps what you need is:

SelectedColor = [[(ColorPickerView *)alertView getColor] retain];

You can only get the RGB color component from a UIColor that is using the RGB color space, since you are using colorWithRed:green:blue:alpha: that is not a problem, but be vary of this if your code changes.

With this is mind getting the color components is really easy:

const CGFloat* components = CGColorGetComponents(SelectedColor.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
NSLog(@"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor));


Related Topics



Leave a reply



Submit