darling-cocotron/AppKit/conversions.h
2020-05-12 17:04:16 -04:00

137 lines
3.8 KiB
C

/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
// NSColorHSBToRGB, NSColorRGBToHSB
// Algorithms derived from: http://en.wikipedia.org/wiki/HSL_and_HSV
#include <CoreGraphics/CGGeometry.h>
static inline void NSColorHSBToRGB(CGFloat hue, CGFloat saturation,
CGFloat brightness, CGFloat *redp,
CGFloat *greenp, CGFloat *bluep)
{
CGFloat red = brightness, green = brightness, blue = brightness;
CGFloat H = hue * 360.f; // convert to degrees
CGFloat S = saturation;
CGFloat V = brightness;
CGFloat m = 0; // the brightness delta.
if (V != 0) {
// We have enough color information to do a conversion
CGFloat C = V * S;
H = fmod(H, 360.f);
H /= 60.f; // convert to hexagonal segments
int hueFace = H; // cube face index
CGFloat X = C * (1 - fabs(fmod(H, 2) - 1));
switch (hueFace) {
case 0:
red = C;
green = X;
blue = 0;
break;
case 1:
red = X;
green = C;
blue = 0;
break;
case 2:
red = 0;
green = C;
blue = X;
break;
case 3:
red = 0;
green = X;
blue = C;
break;
case 4:
red = X;
green = 0;
blue = C;
break;
case 5:
red = C;
green = 0;
blue = X;
break;
}
m = V - C;
}
// Finally add in the brightness delta
*redp = red + m;
*greenp = green + m;
*bluep = blue + m;
// NSLog(@"RGB(%f, %f, %f) <- HSB(%f, %f, %f)", *redp, *greenp, *bluep,
// hue, saturation, brightness);
}
static inline void NSColorRGBToHSB(CGFloat red, CGFloat green, CGFloat blue,
CGFloat *huep, CGFloat *saturationp,
CGFloat *brightnessp)
{
CGFloat M = MAX(red, MAX(green, blue));
CGFloat m = MIN(red, MIN(green, blue));
CGFloat H = 0;
CGFloat S = 0;
CGFloat V = M;
if (V > 0) {
CGFloat C = M - m;
S = C / V;
if (C == 0) {
H = 0;
} else if (red == M) {
H = (green - blue) / C;
H = fmod(H, 6);
} else if (green == M) {
H = 2 + (blue - red) / C;
} else if (blue == M) {
H = 4 + (red - green) / C;
}
H *= 60.f;
if (H < 0) {
H += 360.f;
}
}
if (huep != NULL) {
*huep = H / 360.0;
}
if (saturationp != NULL) {
*saturationp = S;
}
if (brightnessp != NULL) {
*brightnessp = V;
}
// NSLog(@"RGB(%f, %f, %f) -> HSB(%f, %f, %f)", red, green, blue, H/360.f,
// S, V);
}