Problem with NSColors in cocoa

前端 未结 3 570
清酒与你
清酒与你 2021-01-16 11:07

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,         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-16 11:55

    As stated in the documentation:

    Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0

    This means that your values (100,100,100) are going to be converted in (1.0,1.0,1.0) which is white. What you have to do is convert each channel value using the following equation:

    100 : 255 = x : 1.0 => x = 100/255

    where x is the value that you will use for the method

    -(NSColor*)colorWithDeviceRed:CGFloat red green:CGFloat green blue:CGFloat blue alpha:CGFloat alpha];
    

    You should have something like this in your code

    [NSColor colorWithDeviceRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];
    

提交回复
热议问题