博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocoa中颜色的几种表示形式的转换
阅读量:6837 次
发布时间:2019-06-26

本文共 2536 字,大约阅读时间需要 8 分钟。

http://www.goodldy.com/2011/08/cocoa-color/

 

最近频繁用到颜色,整理了以下几个函数用于

NSColor,16进制,整形间的转换

@interface NSColor(NSColorHexadecimalValue)-(NSString*)hexadecimalValueOfAnNSColor;-(unsigned int)hexadecimalINTValueOfAnNSColor;+(NSColor *)getColorFromhexStr:(NSString *)hexColor;@end @implementation NSColor(NSColorHexadecimalValue) //NSColor 转为整形表示 和  16 进制字符串-(unsigned int)hexadecimalINTValueOfAnNSColor{    NSString *tempStr=[self hexadecimalValueOfAnNSColor];    if (tempStr)    {        unsigned int tempInt = 0;        NSScanner *scanner = [NSScanner scannerWithString: tempStr];        [scanner scanHexInt: &tempInt];        return tempInt;    }    else        return 0;}//NSColor 转为 16 进制字符串-(NSString*)hexadecimalValueOfAnNSColor{    float redFloatValue, greenFloatValue, blueFloatValue;    int redIntValue, greenIntValue, blueIntValue;    NSString *redHexValue, *greenHexValue, *blueHexValue;     NSColor *convertedColor=[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];     if(convertedColor)    {        [convertedColor getRed:&redFloatValue green:&greenFloatValue blue:&blueFloatValue alpha:NULL];         redIntValue=redFloatValue*255.99999f;        greenIntValue=greenFloatValue*255.99999f;        blueIntValue=blueFloatValue*255.99999f;         redHexValue=[NSString stringWithFormat:@"%02x", redIntValue];        greenHexValue=[NSString stringWithFormat:@"%02x", greenIntValue];        blueHexValue=[NSString stringWithFormat:@"%02x", blueIntValue];         return=[NSString stringWithFormat:@"%@%@%@", redHexValue, greenHexValue, blueHexValue];     }    return nil; } //16 进制字符串  转成 NSColor+(NSColor *)getColorFromhexStr:(NSString *)hexColor{    unsigned int red, green, blue,alpha;    NSRange range;    range.length = 2;    range.location = 0;     if ([hexColor length]==6)        alpha = 255;    else if ([hexColor length]==8)    {        [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&alpha];        range.location+=2;    }    else        return nil;          [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];    range.location += 2;    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];    range.location += 2;    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];         return [NSColor colorWithDeviceRed:(float)(red/255.0f) green:(float)(green/255.0f)  blue:(float)(blue/255.0f) alpha:(float)(alpha/255.0f) ];} @end

转载于:https://www.cnblogs.com/pengyingh/articles/2451218.html

你可能感兴趣的文章
【笔记】程序员的思维修炼4
查看>>
Scanner 的练习 。。。。依然不懂用法。。。苦恼
查看>>
使用div模拟出frameset效果
查看>>
linux理论知识点(用于考试)
查看>>
Eclipse 导入 Android studio Exception Ljava/lang/UnsatisfiedLinkEror
查看>>
Android Studio 打包签名发布New Key Store
查看>>
01-查看系统整体性能情况:sar
查看>>
privot函数使用
查看>>
Beginning Silverlight 4 in C#-Silverlight控件
查看>>
用户及场景分析
查看>>
Tool.js
查看>>
Java线程池 ExecutorService
查看>>
Windows Phone 8: Evolution of the Runtime and Application Compatibility
查看>>
NOIP 跳石头
查看>>
那些有趣的博客
查看>>
类和对象的定义
查看>>
Java-GC-标记清除算法
查看>>
(转载)Java多线程入门(一)
查看>>
[C#]中获取当前程序运行路径的方法
查看>>
我的第一天
查看>>