可可中NSColors的问题 [英] Problem with NSColors in cocoa

查看:110
本文介绍了可可中NSColors的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSColor构成颜色,当我尝试使用以下值创建RGB颜色时,它只会显示白色:

i am trying to compose colors using NSColor and when i am trying to create RGB color with the following values it just displays the white colors instead:

(r,g,b):(50,50,50)
(r,g,b):(100,100,100)
(r,g,b):(150,150,150)
(r,g,b):(200,200,200)

等...

用于创建颜色的代码是:

the code used to create the colors is:

//用于生成具有背景色的简单图像的代码

// the code to genearet simple images with background colors

NSColor * myColor = [NSColor colorWithDeviceRed:100.0 green:100.0 blue:100.0 alpha:1.0];
NSImage* image1 = [[NSImage alloc] initWithSize:NSMakeSize(10.0,  100.0)];
NSRect imageBounds1 = NSMakeRect (0, 0, 10, 100);

[image1 lockFocus];
[myColor set];
NSRectFill (imageBounds1);
[image1 unlockFocus];

我在网络上找不到任何资源或示例,这可以为我上面的查询提供某种帮助.如果有人可以分享我的智慧,我将非常感激.

I couldn't find any resource or sample on the web, which provides some sort of help on my above queries.It's highly appreciated if someone could share his wisdom on how I can achieve this.

先谢谢了.

推荐答案

NSColor组件的值在[0..1]中,因此您应该标准化自己的值,例如:

NSColor components have values in [0..1] so you should normalize the values you have, e.g.:

NSColor * myColor = [NSColor colorWithDeviceRed:100.0/255 green:100.0/255 blue:100.0/255 alpha:1.0];

如果您尝试为颜色分量设置大于1的值,那么它们将被解释为1,因此您的代码实际上等同于

If you try to set values greater then 1 to colour components then they're interpreted as 1, so your code will be actually equivalent to

NSColor * myColor = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0];

会产生白色.

这篇关于可可中NSColors的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆