Search code examples
iosobjective-ccocoa-touchuiviewuicolor

RGB color incorrect?


I have a very simple method that sets an r,g, and b color. It is the following:

-(void)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{
    r = red;
    g = green;
    b = blue;

    NSLog(@"R: %f, G: %f, B: %f",r,g,b);
}

r,g,b, and o (opacity) are float properties in my custom subclass of uiview. I utilize draw rect like the following:

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, b, g, o);

When I call the method I do it like this:

[self.drawingBox colorWithRed:255.0/255.0 green:59.0/255.0 blue:48.0/255.0];
NSLOG: R: 1.000000, G: 0.231373, B: 0.188235

This works correctly and returns a red color with which I draw.

Unfortunately, whenever I call anything else, it doesn't return the desired color even though it's nslogging the correct color. For example,

[self.drawingBox colorWithRed:255.0/255.0 green:149.0/255.0 blue:0.0/255.0];

Returns a purple color when it should actually be orange. When it gets nslogged, this is what logs for the orange color:

R: 1.000000, G: 0.584314, B: 0.000000

Any ideas on why the second time it returns purple instead of orange (the nslog is the correct color)?


Solution

  • You are passing the parameters to CGContextSetRGBStrokeColor in the wrong order. Try this:

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);