NS颜色|从 RGB 值创建颜色

发布于 2024-10-19 05:15:28 字数 706 浏览 2 评论 0原文

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

这是我编写的函数

+(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];
}

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

r = 187, 克=170, b = 170,

它应该绘制浅灰色,但我得到了完整的白色,在这种情况下,

任何人都知道我做错了什么,

亲切的问候

Rohan

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

划一舟意中人 2024-10-26 05:15:28

该代码对我有用。尝试调试,您​​是否记得在创建颜色后对颜色调用 -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);
}

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);
}
夜血缘 2024-10-26 05:15:28

如果您传递的输入分量超出 255 个,并且出于安全目的希望将其限制在 255 个以内,您可以尝试以下操作:

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

使用 % 值代替除法。

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.

遇见了你 2024-10-26 05:15:28

Swift 2 中的扩展

您可以使用此扩展,它接受来自 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
}

}

使用示例:

let myColor = NSColor.fromRGB(140, green: 150, blue: 155)
let mySemiTranspColor = NSColor.fromRGB(140, green: 150, blue: 155, alpha: 50)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文