Search code examples
objective-ccocoa-touchiosuicolorhsb

UIColor does not match standard RGB color wheel?


Hue in UIColor is not the same as a standard RGB color wheel. What I mean by that is, two opposing colors on a color wheel will match (i.e., yellow and purple), but if you take two opposing colors in UIColor, for example:

Color 1: Hue 0.45
Color 2: Hue 0.95

they don't match. Is there any way to find the opposite color in the spectrum through UIColor?


Solution

  • Colors opposite each other on a wheel not usually called "matching", they are called "complementary", as in, if you mix them together, you get white (or black if you're dealing with subtractive colors, i.e., pigments).

    RGB isn't really a color "wheel" -- it's more of a cube shape. The Hue in HSB, however, is often expressed as an angle, from 0˚ to 360˚, with red at 0˚, and turquoise at 180˚. If you take a value 180˚ from whatever you start with, you get the complement: Red 0˚/turquoise 180˚, green 120˚/purple 300˚, etc. When programming, the range of hue is usually written as a fraction between 0 and 1 inclusive. In that scheme, you can do

    fmod(val - 0.5, 1.0)
    

    to get the complement.

    You haven't said what result you're expecting and what result you're getting, so it's hard to be specifically helpful, but 0.95 and 0.45 are indeed complementary, and when I create UIColors with those values, I see the expected reddish and turquoise-ish hues.

    The colors on a computer screen behave differently than the colors of paint, because in the first case, you are adding wavelengths of light to get a result, and in the second, you are selectively absorbing wavelengths. This may be the source of your confusion. Red plus yellow equals orange with pigments, but red plus green equals yellow with light.