NSColor | Creating Color from RGB Values
Asked Answered
R

3

8

In my application, i will get RGB Values as a unsigned character so it will not be more then 255, I am using NSColor API to create the color and will make use of it to draw the font and background color,

this is the function that i have written

+(NSColor *)getColorFromRGB:(unsigned char)r blue:(unsigned char)b green:(unsigned char)g
{
    CGFloat rFloat = r/255.0;
    CGFloat gFloat = g/255.0;
    CGFloat bFloat = b/255.0;

    //  return [NSColor colorWithCalibratedRed:((float)r/255.0) green:((float)g/255.0) blue:((float)b/255.0) alpha:1.0];
    return [NSColor colorWithCalibratedRed:rFloat green:gFloat blue:bFloat alpha:1.0];
}

In almost all case, when i compare the Color using my RGB Value in RGB palate, color is not matching, For example, when i pass ,

r = 187, g = 170, b = 170,

It should draw the light gray, but i am getting complete whilte color, in this case,

anyone has an idea, what i am doing wrong,

Kind Regards

Rohan

Recapitulation answered 23/2, 2011 at 12:0 Comment(2)
You sure your monitor is color calibrated?Lucilucia
In which scale are you passing the input components? Is it out of 255?Whaleboat
W
5

If you are passing the input components out of 255 and you want to restrict it within 255 for safety purpose, you can try this:

CGFloat rFloat = r % 255.0; CGFloat gFloat = g % 255.0; CGFloat bFloat = b % 255.0;

Instead of divide use % value.

Whaleboat answered 24/2, 2011 at 8:37 Comment(2)
Safety has nothing to do with it. Using % will produce values out of range, as the +colorWithCalibratedRed:green:blue:alpha method accepts values in the range 0-1.Surcharge
In the answer I have mentioned that if you pass input values above 255 and then if you want to restrict it between 255 use %. Also, %255 will not give out of range value. Eg. 177%255 will give 177Whaleboat
S
11

The code works for me. Try debugging, did you remember to call -set on your color after creating it? Here is some example code that works:

static NSColor *colorFromRGB(unsigned char r, unsigned char g, unsigned char b)
{
    return [NSColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0];
}

...

- (void)drawRect:(NSRect)rect {
    NSColor *c = colorFromRGB(50, 100, 255);
    [c set]; // <-- Important
    NSRectFill(rect);
}
Surcharge answered 24/2, 2011 at 9:34 Comment(2)
Thanks Dietrich, same mistake i did :(Recapitulation
Even better is if you can make this colorFromRGBA, add a double a parameter, and then assign it to the alpha: value.Drusi
W
5

If you are passing the input components out of 255 and you want to restrict it within 255 for safety purpose, you can try this:

CGFloat rFloat = r % 255.0; CGFloat gFloat = g % 255.0; CGFloat bFloat = b % 255.0;

Instead of divide use % value.

Whaleboat answered 24/2, 2011 at 8:37 Comment(2)
Safety has nothing to do with it. Using % will produce values out of range, as the +colorWithCalibratedRed:green:blue:alpha method accepts values in the range 0-1.Surcharge
In the answer I have mentioned that if you pass input values above 255 and then if you want to restrict it between 255 use %. Also, %255 will not give out of range value. Eg. 177%255 will give 177Whaleboat
C
3

Extension in Swift 2

You can use this extension which accepts RGB (0-255) from Photoshop, Sketch, etc. and returns a proper NSColor instance (please mind that some source code lines are wider than displayed here):

extension NSColor {

// returns color instance from RGB values (0-255)
static func fromRGB(red: Double, green: Double, blue: Double, alpha: Double = 100.0) -> NSColor {

    let rgbRed = CGFloat(red/255)
    let rgbGreen = CGFloat(green/255)
    let rgbBlue = CGFloat(blue/255)
    let rgbAlpha = CGFloat(alpha/100)

    let color = NSColor(red: rgbRed, green: rgbGreen, blue: rgbBlue, alpha: rgbAlpha)
    return color
}

}

Example use:

let myColor = NSColor.fromRGB(140, green: 150, blue: 155)
let mySemiTranspColor = NSColor.fromRGB(140, green: 150, blue: 155, alpha: 50)
Cyclonite answered 17/2, 2016 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.