Color::Color(CGColorRef color) { if (!color) { m_color = 0; m_valid = false; return; } size_t numComponents = CGColorGetNumberOfComponents(color); const CGFloat* components = CGColorGetComponents(color); float r = 0; float g = 0; float b = 0; float a = 0; switch (numComponents) { case 2: r = g = b = components[0]; a = components[1]; break; case 4: r = components[0]; g = components[1]; b = components[2]; a = components[3]; break; default: ASSERT_NOT_REACHED(); } m_color = makeRGBA(r * 255, g * 255, b * 255, a * 255); m_valid = true; }
Color::Color(CGColorRef color) { if (!color) { m_color = 0; m_valid = false; return; } #if !PLATFORM(IOS) size_t numComponents = CGColorGetNumberOfComponents(color); const CGFloat* components = CGColorGetComponents(color); #else RetainPtr<CGColorRef> correctedColor = adoptCF(createCGColorWithDeviceRGBA(color)); if (!correctedColor) correctedColor = color; size_t numComponents = CGColorGetNumberOfComponents(correctedColor.get()); const CGFloat* components = CGColorGetComponents(correctedColor.get()); #endif // !PLATFORM(IOS) float r = 0; float g = 0; float b = 0; float a = 0; switch (numComponents) { case 2: r = g = b = components[0]; a = components[1]; break; case 4: r = components[0]; g = components[1]; b = components[2]; a = components[3]; break; default: ASSERT_NOT_REACHED(); } m_color = makeRGBA(r * 255, g * 255, b * 255, a * 255); m_valid = true; }
ColorComponents colorComponentsFromColor(SharedColor color) { if (!color) { // Empty color object can be considered as `clear` (black, fully transparent) color. return ColorComponents {0, 0, 0, 0}; } auto numberOfComponents = CGColorGetNumberOfComponents(color.get()); assert(numberOfComponents == 4); const CGFloat *components = CGColorGetComponents(color.get()); return ColorComponents { (float)components[0], (float)components[1], (float)components[2], (float)components[3] }; }
GiColor giFromCGColor(CGColorRef color) { int num = CGColorGetNumberOfComponents(color); CGColorSpaceModel space = CGColorSpaceGetModel(CGColorGetColorSpace(color)); const CGFloat *rgba = CGColorGetComponents(color); if (space == kCGColorSpaceModelMonochrome && num >= 2) { UInt8 c = (UInt8)mgRound(rgba[0] * 255); return GiColor(c, c, c, (UInt8)mgRound(rgba[1] * 255)); } if (num < 3) { return GiColor::Invalid(); } return GiColor((UInt8)mgRound(rgba[0] * 255), (UInt8)mgRound(rgba[1] * 255), (UInt8)mgRound(rgba[2] * 255), (UInt8)mgRound(CGColorGetAlpha(color) * 255)); }