|
| 1 | +// |
| 2 | +// ORBColor.cpp |
| 3 | +// OpenRenderBox |
| 4 | + |
| 5 | +#include <OpenRenderBox/ORBColor.h> |
| 6 | +#include <OpenRenderBoxCxx/Color/ColorSpace.hpp> |
| 7 | +#include <math.h> |
| 8 | + |
| 9 | +using namespace ORB; |
| 10 | + |
| 11 | +// Global color constants |
| 12 | +const ORBColor ORBColorClear = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 13 | +const ORBColor ORBColorBlack = { 0.0f, 0.0f, 0.0f, 1.0f }; |
| 14 | +const ORBColor ORBColorWhite = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 15 | +const ORBColor ORBColorNull = { ORBColorInvalidComponent, ORBColorInvalidComponent, ORBColorInvalidComponent, ORBColorInvalidComponent }; |
| 16 | +const float ORBColorInvalidComponent = -32768.0f; |
| 17 | + |
| 18 | +// sRGB to linear constants |
| 19 | +static const float kSRGBToLinearThreshold = 0.04045f; |
| 20 | +static const float kSRGBToLinearScale = 12.92f; |
| 21 | +static const float kSRGBToLinearGammaOffset = 0.055f; |
| 22 | +static const float kSRGBToLinearGammaScale = 1.055f; |
| 23 | +static const float kSRGBToLinearGamma = 2.4f; |
| 24 | + |
| 25 | +// Linear to sRGB constants |
| 26 | +static const float kLinearToSRGBThreshold = 0.0031308f; |
| 27 | +static const float kLinearToSRGBScale = 12.92f; |
| 28 | +static const float kLinearToSRGBGammaScale = 1.055f; |
| 29 | +static const float kLinearToSRGBGammaOffset = 0.055f; |
| 30 | +static const float kLinearToSRGBGamma = 1.0f / 2.4f; |
| 31 | + |
| 32 | +static inline float sRGBToLinear(float value) { |
| 33 | + float absValue = value > 0.0f ? value : -value; |
| 34 | + float result; |
| 35 | + if (absValue <= kSRGBToLinearThreshold) { |
| 36 | + result = absValue / kSRGBToLinearScale; |
| 37 | + } else { |
| 38 | + result = powf((absValue + kSRGBToLinearGammaOffset) / kSRGBToLinearGammaScale, kSRGBToLinearGamma); |
| 39 | + } |
| 40 | + return value < 0.0f ? -result : result; |
| 41 | +} |
| 42 | + |
| 43 | +static inline float linearToSRGB(float value) { |
| 44 | + float absValue = value > 0.0f ? value : -value; |
| 45 | + float result; |
| 46 | + if (absValue <= kLinearToSRGBThreshold) { |
| 47 | + result = absValue * kLinearToSRGBScale; |
| 48 | + } else { |
| 49 | + result = kLinearToSRGBGammaScale * powf(absValue, kLinearToSRGBGamma) - kLinearToSRGBGammaOffset; |
| 50 | + } |
| 51 | + return value < 0.0f ? -result : result; |
| 52 | +} |
| 53 | + |
| 54 | +ORBColor ORBColorMake(float red, float green, float blue, float alpha) { |
| 55 | + return (ORBColor){ red, green, blue, alpha }; |
| 56 | +} |
| 57 | + |
| 58 | +ORBColor ORBColorMakeLinear(float red, float green, float blue, float alpha) { |
| 59 | + return (ORBColor){ |
| 60 | + sRGBToLinear(red), |
| 61 | + sRGBToLinear(green), |
| 62 | + sRGBToLinear(blue), |
| 63 | + alpha |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +ORBColor ORBColorToLinear(ORBColor color) { |
| 68 | + return (ORBColor){ |
| 69 | + sRGBToLinear(color.red), |
| 70 | + sRGBToLinear(color.green), |
| 71 | + sRGBToLinear(color.blue), |
| 72 | + color.alpha |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +ORBColor ORBColorFromLinear(ORBColor color) { |
| 77 | + return (ORBColor){ |
| 78 | + linearToSRGB(color.red), |
| 79 | + linearToSRGB(color.green), |
| 80 | + linearToSRGB(color.blue), |
| 81 | + color.alpha |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +bool ORBColorEqualToColor(ORBColor lhs, ORBColor rhs) { |
| 86 | + return lhs.red == rhs.red && |
| 87 | + lhs.green == rhs.green && |
| 88 | + lhs.blue == rhs.blue && |
| 89 | + lhs.alpha == rhs.alpha; |
| 90 | +} |
| 91 | + |
| 92 | +#if ORB_TARGET_OS_DARWIN |
| 93 | + |
| 94 | +#include <CoreGraphics/CGColorSpace.h> |
| 95 | + |
| 96 | +ORBColor ORBColorFromComponents(CGColorSpaceRef colorSpace, const CGFloat *components, bool premultiplied) { |
| 97 | + size_t componentCount = premultiplied ? 2 : 1; |
| 98 | + return ORBColorFromComponents2(colorSpace, components, componentCount); |
| 99 | +} |
| 100 | + |
| 101 | +ORBColor ORBColorFromComponents2(CGColorSpaceRef colorSpace, const CGFloat *components, size_t componentCount) { |
| 102 | + // TODO: Implement proper color space conversion |
| 103 | + (void)colorSpace; |
| 104 | + (void)componentCount; |
| 105 | + return (ORBColor){ |
| 106 | + static_cast<float>(components[0]), |
| 107 | + static_cast<float>(components[1]), |
| 108 | + static_cast<float>(components[2]), |
| 109 | + static_cast<float>(components[3]) |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | +ORBColor ORBColorFromCGColor(CGColorRef color, bool premultiplied) { |
| 114 | + CGColorSpaceRef colorSpace = CGColorGetColorSpace(color); |
| 115 | + const CGFloat *components = CGColorGetComponents(color); |
| 116 | + size_t componentCount = premultiplied ? 2 : 1; |
| 117 | + return ORBColorFromComponents2(colorSpace, components, componentCount); |
| 118 | +} |
| 119 | + |
| 120 | +ORBColor ORBColorFromCGColor2(CGColorRef color, size_t componentCount) { |
| 121 | + CGColorSpaceRef colorSpace = CGColorGetColorSpace(color); |
| 122 | + const CGFloat *components = CGColorGetComponents(color); |
| 123 | + return ORBColorFromComponents2(colorSpace, components, componentCount); |
| 124 | +} |
| 125 | + |
| 126 | +CGColorRef ORBColorCopyCGColor(ORBColor color, ORBColorSpace orbColorSpace) { |
| 127 | + CGFloat components[4] = { |
| 128 | + static_cast<CGFloat>(color.red), |
| 129 | + static_cast<CGFloat>(color.green), |
| 130 | + static_cast<CGFloat>(color.blue), |
| 131 | + static_cast<CGFloat>(color.alpha) |
| 132 | + }; |
| 133 | + bool isRedOutOfRange = (color.red < 0.0f || color.red > 1.0f); |
| 134 | + bool isGreenOutOfRange = (color.green < 0.0f || color.green > 1.0f); |
| 135 | + bool isBlueOutOfRange = (color.blue < 0.0f || color.blue > 1.0f); |
| 136 | + ORB::ColorSpace colorSpace = orb_color_space(orbColorSpace).value_or(ORB::ColorSpace::LinearSRGB); |
| 137 | + bool extended = isRedOutOfRange || isGreenOutOfRange || isBlueOutOfRange; |
| 138 | + CGColorSpaceRef cgColorSpace = ORB::cg_color_space(colorSpace, extended); |
| 139 | + CGColorRef cgColor = CGColorCreate(cgColorSpace, components); |
| 140 | + return cgColor; |
| 141 | +} |
| 142 | + |
| 143 | +#endif /* ORB_TARGET_OS_DARWIN */ |
0 commit comments