UIColor各种颜色转换

1、Hex值颜色转换

#import <UIKit/UIKit.h>

@interface UIColor (Extension)

// 根据无符号的32位整数转换为对应的RGB颜色
+ (instancetype)sy_colorWithHex:(u_int32_t)hex;

@end

#import "UIColor+Extension.h"

@implementation UIColor (Extension)

+ (instancetype)sy_colorWithHex:(u_int32_t)hex{

    int red = (hex & 0xFF0000) >> 16;
    int green = (hex & 0x00FF00) >> 8;
    int blue = hex & 0x0000FF;

    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}

@end

2、将UIColor转换为RGB值

- (NSMutableArray *) changeUIColorToRGB:(UIColor *)color
{
    NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];
    NSString *RGBStr = nil;  

    //获得RGB值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];  

    //将RGB值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];

    //获取红色值
    int r = [[RGBArr objectAtIndex:1] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",r];
    [RGBStrValueArr addObject:RGBStr];  

    //获取绿色值
    int g = [[RGBArr objectAtIndex:2] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",g];
    [RGBStrValueArr addObject:RGBStr];  

    //获取蓝色值
    int b = [[RGBArr objectAtIndex:3] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",b];
    [RGBStrValueArr addObject:RGBStr];  

    //返回保存RGB值的数组
    return [RGBStrValueArr autorelease];
}

3、16进制颜色(html颜色值)字符串转为UIColor

+(UIColor *) hexStringToColor: (NSString *) stringToConvert
{
     NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
     // String should be 6 or 8 characters  

     if ([cString length] < 6) return [UIColor blackColor];
     // strip 0X if it appears
     if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
     if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
     if ([cString length] != 6) return [UIColor blackColor];  

     // 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];
     // Scan values
     unsigned int r, g, b;  

     [[NSScanner scannerWithString:rString] scanHexInt:&r];
     [[NSScanner scannerWithString:gString] scanHexInt:&g];
     [[NSScanner scannerWithString:bString] scanHexInt:&b];  

     return [UIColor colorWithRed:((float) r / 255.0f)
                                 green:((float) g / 255.0f)
                                   blue:((float) b / 255.0f)
                                 alpha:1.0f];
}
时间: 2024-11-05 12:29:40

UIColor各种颜色转换的相关文章

iOS中RGB颜色转换

iOS中RGB常用的色值,同时可将对颜色的设置定义成宏,方便开发应用,如: // RGB颜色转换(16进制->10进制) #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue 

iOS开发-16进制颜色转换

项目中经常会用到颜色转换,有的是通过十六进制转成数字转颜色,想简单的点直接通过字符串转一下,简单扩展了一下分类UIColor,代码如下: +(UIColor *)colorWithHex:(NSString *)hexColor{ return [self colorWithHex:hexColor alpha:1.0f]; } //http://www.cnblogs.com/xiaofeixiang iOS技术交流:228407086 +(UIColor *)colorWithHex:(NS

十六进制颜色转换RGB对照表

十六进制颜色转换RGB对照表 版权声明:本文为博主原创文章,未经博主允许不得转载.

C语言颜色转换宏

C语言颜色转换宏 #define COLOR_BPP16_RGB555 /* Win RGB */ #define COLOR_RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))) #define COLOR_RGBA(r,g,b,a) ((COLORREF)((((DWORD)(BYTE)(a))<<24)|COLOR_RGB(r,g,b))) /* W

javascript把RGB指定颜色转换成十六进制颜色(Converting R,G,B values to HTML hex notation)

Prologue 看见一篇很好的外国文章,Making annoying rainbows in javascript,其实我当时很想把它翻译下来的,但是对于一个连六级都没过的人确实有点难度,一些文段看起来是知道意思但是就不知道怎么用自己的话来表达.越来越觉得对于IT行业来说英语至少要达到能读的程度,毕竟好的东西很多是外国的,还有哦,要会翻墙.对于那篇文章我其实还没看完,还在慢慢消化中,但是看到一个函数确实真的很吸引我,实现的是Converting R,G,B values to HTML he

WPF 颜色转换

原文:WPF 颜色转换 从字符串到画刷: var converter = new System.Windows.Media.BrushConverter(); var brush = (Brush)converter.ConvertFromString("#FFFFFF90"); Fill = brush; 原文地址:https://www.cnblogs.com/lonelyxmas/p/9559548.html

[iOS]把16进制(#871f78)颜色转换UIColor

// // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights reserved. // #import <UIKit/UIKit.h> #import "ViewController.h" // 定义个宏,方便实用,>> 16 的意思是把16进制转换成2进制后,右移16位(移动掉的直接舍弃) #define UIColorF

颜色转换:#hhhfff-&gt;UIColor (MHHexColoring)

MHHexColoring为开发者快速获取想要的十六进制颜色(Hex Color) 查找16进制色码的网站:http://www.color-hex.com // 版权属于原作者 MHHexColoring http://code4app.com/ios/MHHexColoring/548e9485933bf0a9738b6301 1.使用方法: 加入UIColor+HexString.h/m文件,导入头文件: #import "UIColor+HexString.h" 获取颜色,返回

[iOS]把16进制颜色转换UIColor

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIColor *color = [self colorWithHexString:@"#81c52b"]; [self.view setBackgroundColor:color]; } - (UIColor *) colorWithHexString: