Problem with NSColors in cocoa

前端 未结 3 566
清酒与你
清酒与你 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:47

    If I recall correctly you'll want the range 0-1 as your RGB as well

    0 讨论(0)
  • 2021-01-16 11:48

    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];
    

    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];
    

    Which creates white colour.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题