MAC版颜色查看器

该软件为MAC版电脑版开发,功能用于显示查询RGB颜色和十六进制颜色

软件功能很简单,就是通过rgb颜色来创建一个显示颜色的view即可。可以输入rgb颜色,也可以输入十六进制的颜色。

当你看到代码里面别人已经写好的rgb颜色,就不知道别人代码里面指定的颜色是什么,之前一直都是在网上在线查这个颜色,今天找的烦了,索性就自己写一个。

源码下载:

github地址:https://github.com/DamonHu/checkColor

gitosc地址:http://git.oschina.net/DamonHoo/checkColor

软件下载

最新版软件下载:http://www.huaimayi.com/portal.php?mod=view&aid=18

软件说明:

软件功能很简单,就是通过rgb颜色来创建一个显示颜色的view即可。可以输入rgb颜色,也可以输入十六进制的颜色

屏幕快照 2016-04-28 16.49.21.png

RGB颜色查询

屏幕快照 2016-04-28 16.49.39.png

十六进制颜色查询

这两个适应不同的需要,因为怕误点清空颜色这个按钮,所以把清空颜色这个快捷按钮设置到最左边。

核心代码

核心代码就是获取颜色,并且显示

-(void)checkColor{
    float redColor = 0.0;
    float greenColor = 0.0;
    float blueColor = 0.0;
    NSColor *color;
    if ((![[self.redTextField stringValue] isEqual: @""])&&(![[self.redTextField stringValue] isEqual: @""])&&(![[self.redTextField stringValue] isEqual: @""])) {
       redColor = [[self.redTextField stringValue] floatValue]/255.0;
       greenColor  = [[self.greenTextField stringValue] floatValue]/255.0;
       blueColor  = [[self.blueTextField stringValue] floatValue]/255.0;
       color =[NSColor colorWithRed:redColor green:greenColor blue:blueColor alpha:1.0f];
    }
    else if (![[self.hexTextField stringValue] isEqual:@""])
    {
        color = [self getColor:[self.hexTextField stringValue]];
        NSLog(@"%@",color);
    }
    else{
        NSError * error = [NSError errorWithDomain:@"请输入RGB颜色值或者16进制颜色值" code:1 userInfo:NULL];
        NSAlert * alert = [NSAlert alertWithError:error];
        [alert runModal];
        return;
    }
    [self.colorView.layer setBackgroundColor:color.CGColor];
}
//16进制获取颜色
-(NSColor*) getColor:(NSString*)hexColor
{
    unsigned int red=0,green=0,blue=0;
    NSRange range;
    range.length = 2;

    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];

    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];

    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];

    return [NSColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

这个16进制获取颜色的方法参照的这个文章:IOS开发之----将16进制颜色转换成RGB值

这个文章还说了用cocos获取十六进制颜色的方法,这个暂时没有使用,贴上来仅供参考。

+ (ccColor3B)colorWithHexString: (NSString *) stringToConvert
{ 
//去掉前后空格换行符 
 NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; 

 if ([cString length] < 6) 
 return ccWHITE; 

if ([cString hasPrefix:@"0X"]) 
cString = [cString substringFromIndex:2]; 
 else if ([cString hasPrefix:@"#"]) 
cString = [cString substringFromIndex:1]; 

 if ([cString length] != 6) 
return ccWHITE; 

// Separate into r, g, b substrings 
 NSRange range; 
range.location = 0; 
range.length = 2; 
 NSString *rString = [cString substringWithRange:range]; 

 range.location = 2; 
NSString *gString = [cString substringWithRange:range]; 

 range.location = 4; 
 NSString *bString = [cString substringWithRange:range]; 

 unsigned int r, g, b; 
 [[NSScanner scannerWithString:rString] scanHexInt:&r]; 
 [[NSScanner scannerWithString:gString] scanHexInt:&g]; 
 [[NSScanner scannerWithString:bString] scanHexInt:&b]; 
 //NSLog(@"r = %u, g = %u, b = %u",r, g, b); 
 return ccc3(r, g, b); 
 }

互相学习,互相进步

Last modification:June 21st, 2017 at 08:03 pm
如果看了这个文章可以让你少加会班,可以请我喝杯可乐
已打赏名单
微信公众号

Leave a Comment