macos NSColor | 从 RGB 值创建颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5090603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:44:42  来源:igfitidea点击:

NSColor | Creating Color from RGB Values

objective-ccocoamacosnscolor

提问by Amitg2k12

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,

在我的应用程序中,我将获得 RGB 值作为无符号字符,因此它不会超过 255,我使用 NSColor API 创建颜色并使用它来绘制字体和背景颜色,

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 ,

在几乎所有情况下,当我使用 RGB 味觉中的 RGB 值比较颜色时,颜色不匹配,例如,当我通过时,

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

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

罗汉

采纳答案by spd

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:

如果您将输入组件从 255 中传递出去,并且出于安全目的希望将其限制在 255 内,您可以尝试以下操作:

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

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

Instead of divide use % value.

使用 % 值代替除法。

回答by Dietrich Epp

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

该代码对我有用。尝试调试,您​​是否记得-set创建后调用您的颜色?这是一些有效的示例代码:

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);
}

回答by Sebastian

Extension in Swift 2

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):

您可以使用此扩展程序,它接受来自 Photoshop、Sketch 等的 RGB (0-255) 并返回正确的 NSColor 实例(请注意,某些源代码行比此处显示的要宽):

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)