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] }; }
static QColor qcolorFromCGColor(CGColorRef cgcolor) { QColor pc; CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(cgcolor)); const CGFloat *components = CGColorGetComponents(cgcolor); if (model == kCGColorSpaceModelRGB) { pc.setRgbF(components[0], components[1], components[2], components[3]); } else if (model == kCGColorSpaceModelCMYK) { pc.setCmykF(components[0], components[1], components[2], components[3]); } else if (model == kCGColorSpaceModelMonochrome) { pc.setRgbF(components[0], components[0], components[0], components[1]); } else { // Colorspace we can't deal with. qWarning("Qt: qcolorFromCGColor: cannot convert from colorspace model: %d", model); Q_ASSERT(false); } return pc; }
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)); }