NSColor |从RGB值创建颜色 [英] NSColor | Creating Color from RGB Values
问题描述
在我的应用程序中,我将获得RGB值作为无符号字符,因此不会超过255,我正在使用NSColor API创建颜色,并将使用它来绘制字体和背景色,
这是我编写的功能
+(NSColor *)getColorFromRGB:(unsigned char)r蓝色:(unsigned char)b绿色:(unsigned char)g{CGFloat rFloat = r/255.0;CGFloat gFloat = g/255.0;CGFloat bFloat = b/255.0;//返回[NSColor colorWithCalibratedRed:((float)r/255.0)green:((float)g/255.0)blue:((float)b/255.0)alpha:1.0];返回[NSColor colorWithCalibratedRed:rFloat绿色:gFloat蓝色:bFloat alpha:1.0];}
几乎在所有情况下,当我使用RGB色板中的RGB值比较颜色时,颜色都不匹配,例如,当我通过时,
r = 187,g = 170,b = 170,
它应该绘制浅灰色,但是在这种情况下,我会得到完全白色的颜色
任何人都有一个主意,我在做什么错了,
亲切的问候
罗汉
如果要从255个输入组件中传递输入数据,并且出于安全考虑,希望将其限制在255个以内,则可以尝试以下操作:
p CGFloat rFloat = r 255.0;CGFloat gFloat = g%255.0;CGFloat bFloat = b%255.0;而不是除以%值.
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
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.
这篇关于NSColor |从RGB值创建颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!