Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

I found an annoying behavior in UIColor today. If either saturation or brightness is 0 when creating an HSB color, other color values are ignored. This results in a valid color, and if drawn, it will be the correct color. But the values coming out of it are incorrect.

Assuming these defined values:

UIColor *color;
CGFloat inputHue, inputSaturation, inputBrightness, inputAlpha;
CGFloat outputHue, outputSaturation, outputBrightness, outputAlpha;

Paramters are ignored if brightness is 0:

inputHue = 0.66;
inputSaturation = 1.0;
inputBrightness = 0.0;
inputAlpha = 1.0;
color = [UIColor colorWithHue:inputHue saturation:inputSaturation brightness:inputBrightness alpha:inputAlpha];
[color getHue:&outputHue saturation:&outputSaturation brightness:&outputBrightness alpha:&outputAlpha];

// Both of these will fail
NSParameterAssert(inputHue == outputHue);
NSParameterAssert(inputSaturation == outputSaturation);

Parameters are ignored if saturation is 0:

inputHue = 0.66;
inputSaturation = 0.0;
inputBrightness = 1.0;
inputAlpha = 1.0;
color = [UIColor colorWithHue:inputHue saturation:inputSaturation brightness:inputBrightness alpha:inputAlpha];
[color getHue:&outputHue saturation:&outputSaturation brightness:&outputBrightness alpha:&outputAlpha];

// This will fail
NSParameterAssert(inputHue == outputHue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment