반응형
UIColor+Hexcode
참 다양한 케이스가 있는 것 같다.
이번에는 웹뷰(UIWebView)에서 Native영역의 상단 타이틀 컬러를 변경해야하는 상황이라 잠시 고민을 했다. 기존에 사용하던 매크로는 다음과 같은 형식으로 정의 되어있다.
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
사용할때는 [view setBackgroundColor:UIColorFromRGB(0xffffff)] 와 같은데 , 웹에서 올라온 color값을 NSString으로 받아서("FFFFFF") 처리할려니 살짝 당황스러웠다.
가지고 있는 리소스들 중에서 찾아보니 적당히 괜찮은 Category Method가 있어서 옮겨보고자 한다.
//
// CATEGORY
//
/**
* Translate a hexcode string ( i.e. #FFFFFF ) to a UIColor instance equivalent
*
* @return UIColor
*
* @example
* NSString *hexcode = @"#FFFFFF";
* UIColor *color = [UIColor colorFromHexCode:hexCode];
*/
+(UIColor *)colorFromHexCode:(NSString *)hexCode;
//
// INSTANCE
//
/**
* Returns a hexcode string ( i.e., #FFFFFF ) for the current color
*
* @return NSString
*
* @example
* UIColor *color = [UIColor redColor];
* NSString *hexColor = [color hexCodeColor];
*/
-(NSString *)hexCodeColor;
/**
* This is some code I wrote for an app a buddy of mine and I work on from time to time called eLBee.
*/
+(UIColor *)colorFromHexCode:(NSString *)colorString {
if (![colorString hasPrefix:@"#"]) {
colorString = [NSString stringWithFormat:@"#%@", colorString];
}
if(colorString.length == 7) {
const char *colorUTF8String = [colorString UTF8String];
int r, g, b;
sscanf(colorUTF8String, "#%2x%2x%2x", &r, &g, &b);
return [UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:1.0];
}
return [UIColor blackColor];
}
/**
* Original Source for code below: https://github.com/nicklockwood/ColorUtils/blob/master/ColorUtils/ColorUtils.m
*
* Note: Made some minor mods... JGH 05/30/2014
*/
//
// ColorUtils.m
//
// Version 1.1.2
//
// Created by Nick Lockwood on 19/11/2011.
// Copyright (c) 2011 Charcoal Design
//
// Distributed under the permissive zlib License
// Get the latest version from here:
//
// https://github.com/nicklockwood/ColorUtils
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//
-(NSString *)hexCodeColor {
CGFloat alpha = CGColorGetAlpha( self.CGColor );
if( alpha < 1.0f ) {
return [NSString stringWithFormat:@"#%.8x", [self RGBAValue]];
}
//don't include alpha component
return [NSString stringWithFormat:@"#%.6x", [self RGBValue]];
}
-(uint32_t)RGBValue {
CGFloat rgba[4];
CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
const CGFloat *components = CGColorGetComponents(self.CGColor);
switch(model) {
case kCGColorSpaceModelMonochrome: {
rgba[0] = components[0];
rgba[1] = components[0];
rgba[2] = components[0];
rgba[3] = components[1];
}
break;
case kCGColorSpaceModelRGB: {
rgba[0] = components[0];
rgba[1] = components[1];
rgba[2] = components[2];
rgba[3] = components[3];
}
break;
default: {
rgba[0] = 0.0f;
rgba[1] = 0.0f;
rgba[2] = 0.0f;
rgba[3] = 1.0f;
}
break;
}
uint32_t red = (uint32_t)(rgba[0]*255);
uint32_t green = (uint32_t)(rgba[1]*255);
uint32_t blue = (uint32_t)(rgba[2]*255);
return (red << 16) + (green << 8) + blue;
}
-(uint32_t)RGBAValue {
CGFloat rgba[4];
CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
const CGFloat *components = CGColorGetComponents(self.CGColor);
switch( model ) {
case kCGColorSpaceModelMonochrome: {
rgba[0] = components[0];
rgba[1] = components[0];
rgba[2] = components[0];
rgba[3] = components[1];
}
break;
case kCGColorSpaceModelRGB: {
rgba[0] = components[0];
rgba[1] = components[1];
rgba[2] = components[2];
rgba[3] = components[3];
}
break;
default: {
rgba[0] = 0.0f;
rgba[1] = 0.0f;
rgba[2] = 0.0f;
rgba[3] = 1.0f;
}
break;
}
uint8_t red = (uint8_t)(rgba[0]*255);
uint8_t green = (uint8_t)(rgba[1]*255);
uint8_t blue = (uint8_t)(rgba[2]*255);
uint8_t alpha = (uint8_t)(rgba[3]*255);
return (red << 24) + (green << 16) + (blue << 8) + alpha;
}
반응형
'프로그래밍 > Xcode-iOS' 카테고리의 다른 글
iOS 위젯 만들기 (Today Extension) 준비 과정. (0) | 2018.11.15 |
---|---|
ios WebView 설정, next focus가능하도록 설정. (0) | 2018.10.19 |
CBPeripheral advertismentData 값 파싱 코드 예제 (0) | 2018.07.11 |
소켓 기반 iOS 프로그래밍 (0) | 2018.05.15 |
diff: /Podfile.lock: No such file or directory (0) | 2018.05.02 |