[NSColor colorWithHTMLName:]のようなHTML表現文字列からCGColorを作成する必要がありますが、CoreGraphicsを使用する場合のみです。
2 に答える
3
次のようなものを試してください。
CGColorRef CGColorFromHTMLString(NSString *str)
{
// remove the leading "#" and add a "0x" prefix
str = [NSString stringWithFormat:@"0x%@", [str substringWithRange:NSMakeRange(1, str.length - 1)]];
NSScanner *scanner;
uint32_t result;
scanner = [NSScanner scannerWithString:str];
[scanner scanHexInt:&result];
CGColorRef color = CGColorCreateGenericRGB(((result >> 16) & 0xff) / 255.0, ((result >> 8) & 0xff) / 255.0, ((result >> 0) & 0xff) / 255.0, 1.0);
return color;
}
使用後は、結果を呼び出すことで結果を解放することを忘れないでくださいCGColorRelease
。
編集:Foundationを使用したくない場合は、CFStringRefまたはプレーンC文字列を試してください。
CGColorRef CGColorFromHTMLString(const char *str)
{
uint32_t result;
sscanf(str + 1, "%x", &result);
CGColorRef color = CGColorCreateGenericRGB(((result >> 16) & 0xff) / 255.0, ((result >> 8) & 0xff) / 255.0, ((result >> 0) & 0xff) / 255.0, 1.0);
return color;
}
于 2012-06-10T17:38:29.847 に答える
1
H2CO3に感謝します!
これがCoreGraphicsソリューションです。つまり、Foundationクラスはなく、CoregraphicsとC++です。
// Remove the preceding "#" symbol
if (backGroundColor.find("#") != string::npos) {
backGroundColor = backGroundColor.substr(1);
}
unsigned int decimalValue;
sscanf(backGroundColor.c_str(), "%x", &decimalValue);
printf("\nstring=%s, decimalValue=%u",backGroundColor.c_str(), decimalValue);
CGColorRef result = CGColorCreateGenericRGB(((decimalValue >> 16) & 0xff) / 255.0, ((decimalValue >> 8) & 0xff) / 255.0, ((decimalValue >> 0) & 0xff) / 255.0, 1.0);
于 2012-06-10T17:57:48.733 に答える