1.ioså¼åUIColorï¼CGColorï¼CIColorä¸è
çåºå«åèç³»
ioså¼åUIColorï¼CGColorï¼CIColorä¸è çåºå«åèç³»
ä¸ãUIColor
ããUIColoræ¯UIKitä¸åå¨é¢è²ä¿¡æ¯çä¸ä¸ªéè¦çç±»ï¼ä¸ä¸ªUIColor对象å å«äºé¢è²åéæ度çå¼ï¼å®çé¢è²ç©ºé´å·²ç»é对IOSè¿è¡äºä¼åãUIColorå å«äºä¸äºç±»æ¹æ³ç¨äºå建ä¸äºæ常è§çé¢è²ï¼å¦ç½è²ï¼é»è²ï¼çº¢è²ï¼éæè²çï¼è¿äºé¢è²çè²å½©ç©ºé´ä¹ä¸å°½ç¸åï¼ç½è²åé»è²æ¯kCGColorSpaceDeviceGrayï¼çº¢è²çè²å½©ç©ºé´æ¯kCGColorSpaceDeviceRGBï¼ã
ããæ¤å¤UIColorè¿æ两个éè¦çå±æ§ï¼ä¸ä¸ªæ¯CGColorï¼ä¸ä¸ªæ¯CIColor(5.0ä¹åæ·»å )ãè¿ä¸¤ä¸ªå±æ§å°±å¯ä»¥æUIColorï¼CGColorï¼CIColorä¸ä¸ªå¯¹è±¡è系起æ¥äºï¼åé¢ä¼è¯¦ç»ä»ç»è¿ä¸è ä¹é´ç转æ¢ã
äºãCGColor
ããCGColor主è¦ç¨äºCoreGaphicsæ¡æ¶ä¹ä¸ï¼CGColorå ¶å®æ¯ä¸ªç»æä½ï¼èæ们é常å¨ä½¿ç¨çCGColorçæ¶å使ç¨çæ¯å®çå¼ç¨ç±»åCGColorRefãCGColor主è¦ç±CGColorSapceåColor
Components两个é¨åç»æï¼åæ ·çé¢è²ç»æï¼å¦æé¢è²ç©ºé´ä¸åçè¯ï¼è§£æåºæ¥çç»æå¯è½ä¼ææä¸åãè¿å°±åæ们å¨å¤çå¾çæ°æ®çæ¶åï¼å¦ææRGBAæ ¼å¼å½æBGRAæ ¼å¼å¤ççç»æå¯æ³èç¥ãå¨Quartz
2Dä¸CGColor常ç¨æ¥è®¾ç½®contextçå¡«å é¢è²ï¼è®¾ç½®éæ度çã
1ãå¦ä½å建ä¸ä¸ªCGColorï¼æ常ç¨çå½æ°æ¯CGColorCreateï¼è¯¥å½æ°æ两个åæ°ï¼
ãã1) colorspaceï¼æå®CGColor对åºçé¢è²ç©ºé´ï¼Quartzå°±ä¼retain该对象ï¼å æ¤è°ç¨å®ä¹åä½ å°±å¯ä»¥å®å ¨çéæ¾è¯¥å¯¹è±¡ã
ãã2) componentsï¼ä¸ä¸ªCGFloatçæ°ç»ï¼è¯¥æ°ç»çå ç´ ä¸ªæ°æ¯æå®è²å½©ç©ºé´å å«çé¢è²åéæ°nï¼å ä¸å¯¹åºçalphaå¼ãã
ãã该å½æ°è¯¥è¿åä¸ä¸ªæ°å建çCGColorRefï¼å½æ们ä¸å使ç¨è¯¥å¯¹è±¡çæ¶å使ç¨CGColorReleaseå½æ°éæ¾è¯¥å¯¹è±¡ã
2ãè·åCGColorçæ°æ®
ããå¨æ们å建çæ¶åä¼ å ¥ä¸¤ä¸ªéè¦çåæ°è¿å»ï¼å½æ们è·åå°äºCGColorRef以åå½ç¶å°±å¯ä»¥æ¿å°å¯¹åºçColorSpace以åComponentsã
ãã1) è·åColorSpace
ããéè¿CGColorGetColorSpaceå½æ°æ们å¯ä»¥è·åå°å½åCGColorRef对åºçColorSpaceï¼è¯¥å½æ°åªæ¥åä¸ä¸ªåæ°å°±æ¯ä½ è¦è·åColorSpaceçCGColorRefãä¸é¢è¯·çä¸ä¸ªç®åçä¾åï¼
CGColorRef cgColor = [UIColor redColor].CGColor;
CGColorSpaceRef colorSpace = CGColorGetColorSpace(cgColor);
NSLog(@"color space: %@",例例linux安装gcc源码 colorSpace);
ãã2) è·åColor Components
ããè¦è·åå°CGColorRef对åºçé¢è²å¼ï¼æ们éè¦ç¨å°CGColorGetNumberOfComponentsåCGColorGetComponents两个å½æ°ãæ们å æ¥çç两个å½æ°çå½æ°ååï¼
size_t CGColorGetNumberOfComponents (
CGColorRef color
);
const CGFloat * CGColorGetComponents (
CGColorRef color
);
ãã第ä¸ä¸ªå½æ°æ¯è·å¾CGColorRefçä¸å å«çé¢è²ç»æé¨åç个æ°ï¼ç¬¬äºä¸ªå½æ°å°±æ¯è·åå®é çé¢è²ç»æé¨åçæ°ç»ï¼ä¸é¢çä¸ä¸ªå°ä¾åï¼
NSUInteger num = CGColorGetNumberOfComponents(cgColor);
const CGFloat *colorComponents = CGColorGetComponents(cgColor);
for (int i = 0; i < num; ++i) {
NSLog(@"color components %d: %f", i, colorComponents[i]);
}
ã
ä¸ãCIColor
ããCIColor主è¦æ¯ç¨äºåCore
Imageæ¡æ¶ä¸å ¶ä»ç±»ï¼æ¯å¦CIFilterï¼CIContext以åCIImageãä»å¤©æ们主è¦å ³å¿çé¢è²å¼é¨åï¼CIColorä¸é¢è²å¼çèå´æ¯0.0-1.0ä¹é´ï¼0.0代表该é¢è²åé为æå°å¼ï¼1.0代表æ¹é¢è²åé为æ大å¼ãå ¶ä¸alphaå¼çèå´ä¹æ¯0.0å°1.0ä¹é´ï¼0.0ä»£è¡¨å ¨éæï¼1.0代表å®å ¨ä¸éæï¼åæ¶CIColorçé¢è²åéé常é½æ¯æ²¡æä¹ä»¥alphaå¼ã
ããæ们å¯ä»¥ä½¿ç¨initWithCGColor:å½æ°ï¼éè¿CGColorå建ä¸ä¸ªCIColorãå ¶ä¸ä¼ å ¥çCGColorRef对象å¯ä»¥ä½¿ä»»ä½ä»»ä½é¢è²ç©ºé´ï¼ä½æ¯Core
Imageæ¡æ¶ä¼å¨ä¼ å ¥filter kernelä¹åæææçé¢è²ç©ºé´è½¬æ¢å°core imageå·¥ä½é¢è²ç©ºé´ãcore
imageå·¥ä½é¢è²ç©ºé´ä½¿ç¨ä¸ä¸ªé¢è²åéå ä¸ä¸ä¸ªalphaåéç»æ(å ¶å®å°±æ¯kCGColorSpaceDeviceRGB)ï¼åé¢çä¾åä¸æ们éªè¯è¿ä¸ç¹ã
åãUIColorï¼CGColorï¼CIColorçåºå«åèç³»
1ãUIColorç两个å±æ§CGColorï¼CIColor
ããUIColorçCGColoræ»æ¯ææçï¼ä¸ç®¡å®æ¯éè¿CGColorï¼CIColorï¼è¿æ¯å ¶ä»æ¹æ³å建çï¼CGColorå±æ§é½æ»æ¯ææçï¼ä½æ¯CIColorå±æ§å°±ä¸æ»æ¯ææçï¼åªæå½UIColoræ¯éè¿CIColorå建çæ¶åï¼ä»ææ¯ææçï¼å¦å访é®è¯¥å±æ§å°ä¼æåºå¼å¸¸ï¼ä¸é¢ç §æ§æ¥ä¸ä¸ªå°ä¾åï¼
// test init uicolor with CGColor
UIColor *color = [UIColor colorWithCGColor:[UIColor whiteColor].CGColor];
// CGColor property is always valid
NSLog(@"CGColor from UIColor %@", color.CGColor);
// don't use CIColor property
// This property throws an exception if the color object was not initialized with a Core Image color.
NSLog(@"CIColor from UIColor %@", color.CIColor); // crush
2ãUIColor使ç¨CGColoråå§å
ããå½UIColor使ç¨CGColoråå§åçæ¶åï¼ææCGColorRefå å«çä¿¡æ¯ï¼é½ä¼è¢«åå°ä¸å¨çä¿çï¼å ¶ä¸å°±å æ¬Color
spaceï¼èä¸éè¿ä¸é¢çå°ä¾åæ们è¿å¯ä»¥çå°å¦æ使ç¨CGColoråå§åUIColorçæ¶åï¼UIColorå ¶å®æ¯ç´æ¥ä¿çäºä¸ä»½è¿ä¸ªCGColorRef对象ãä¾åå¦ä¸ï¼
// test kCGColorSpaceDeviceCMYK
CGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK();
CGFloat cmykValue[] = { 1, 1, 0, 0, 1}; // blue
CGColorRef colorCMYK = CGColorCreate(cmykSpace, cmykValue);
CGColorSpaceRelease(cmykSpace);
NSLog(@"colorCMYK: %@", colorCMYK);
// color with CGColor, uicolor will just retain it
UIColor *color = [UIColor colorWithCGColor:colorCMYK];
NSLog(@"CGColor from UIColor: %@", color.CGColor);
3ãUIColor使ç¨CIColoråå§å
ããä¸é¢æ们讨论ä¸ä¸å½ä½¿ç¨CIColoræ¥åå§åä¸ä¸ªUIColorçæ¶åï¼åå»è®¿é®UIColorçCGColorå±æ§çæ¶åï¼æ们ä¼åç°CGColorçcolor
Spaceå设置CIColorçcolor
spaceçæ¯ä¸å®å ¨ä¸æ ·çï¼å¨è¿ä¸ªè¿ç¨ä¸CIColorä¼ä¸ºæ们åä¸ä¸ªè½¬æ¢ãä¸é¢æ们åå«çç使ç¨kCGColorSpaceDeviceGrayï¼kCGColorSpaceDeviceRGBï¼kCGColorSpaceDeviceCMYKä¸ç§é¢è²ç©ºé´æ¥åå§åä¸ä¸ªCIColorçæ¶åï¼åå»ä½¿ç¨è¯¥CIColorå»åå§åä¸ä¸ªUIColorï¼ç¶åå¨å»è®¿é®å ¶CIColorå±ï¼CGColorå±æ§ï¼æ¥çé¢è²ç©ºé´å¹¶æå°é¢è²ä¿¡æ¯ã
ãã1) 使ç¨kCGColorSpaceDeviceGrayåå§åCIColor
ããé¦å ç代ç ï¼
// test kCGColorSpaceDeviceGray
NSLog(@"CGColor white color:%@", [UIColor whiteColor].CGColor);
CIColor *ciColor = [CIColor colorWithCGColor:[UIColor whiteColor].CGColor];
NSLog(@"cicolor: %@", ciColor);
NSLog(@"cicolor colorspace: %@", ciColor.colorSpace);
color = [UIColor colorWithCIColor:ciColor];
NSLog(@"color %@", color);
// Core Image converts all color spaces to the Core Image working color
// space before it passes the color space to the filter kernel.
// kCGColorSpaceDeviceGray ---> kCGColorSpaceDeviceRGB
NSLog(@"cicolor from UIColor: %@", color.CIColor);
NSLog(@"cicolor's colorspace: %@", color.CIColor.colorSpace);
NSLog(@"color's CGColor: %@", color.CGColor);
ããéè¿è¿è¡ç¨åºï¼æ们çåºæ¥ï¼å¦æ使ç¨ä¸ä¸ªkCGColorSpaceDeviceGrayçé¢è²ç©ºé´çCGColoræ¥åå§åCIColorçæ¶åï¼æ们å¯ä»¥çå°CIColorçè²å½©ç©ºé´ä¸ç´æ¯kCGColorSpaceDeviceGrayï¼éè¿è®¿é®UIColorçCIColorå±æ§ï¼æ们å¯ä»¥çå°å ¶é¢è²ç©ºé´ä»ç¶æ¯kCGColorSpaceDeviceGrayï¼ä½æ¯å½è®¿é®UIColorçCGColorå±æ§çæ¶åï¼éè¿æå°å¯ä»¥åç°å ¶è²å½©ç©ºé´å·²ç»è½¬åæäºkCGColorSpaceDeviceRGB空é´äºï¼èé¢è²å¼ä¹æ£ç¡®çä»åæ¥çé¢è²ç©ºé´è½¬æ¢å°äºæ°çé¢è²ç©ºé´ã
ãã2) 使ç¨kCGColorSpaceDeviceRGBåå§åCIColor
ããåæ ·çæ们ç代ç ï¼
//test kCGColorSpaceDeviceRGB
NSLog(@"CGColor red color:%@", [UIColor redColor].CGColor);
CIColor *ciColor = [CIColor colorWithCGColor:[UIColor redColor].CGColor];
NSLog(@"cicolor: %@", ciColor);
NSLog(@"cicolor colorspace: %@", ciColor.colorSpace);
UIColor *color = [UIColor colorWithCIColor:ciColor];
NSLog(@"color %@", color);
NSLog(@"cicolor from UIColor: %@", color.CIColor);
NSLog(@"cicolor's colorspace: %@", color.CIColor.colorSpace);
NSLog(@"color's CGColor: %@", color.CGColor);
ããæ´ä¸ªè¿ç¨ä¸CIColorï¼ä»¥åéè¿UIColorçCGColoråCIColorå±æ§è®¿é®å°çå¼ï¼æå°åºæ¥æ们å¯ä»¥åç°å®ä»¬é½æ¯kCGColorSpaceDeviceRGB空é´çã
4ã使ç¨kCGColorSpaceDeviceCMYKåå§åCIColor
ããä¸é¢ç»§ç»çä¸æ®µä»£ç ï¼
// test kCGColorSpaceDeviceCMYK
CGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK();
NSLog(@"Components number: %zu", CGColorSpaceGetNumberOfComponents(cmykSpace));
CGFloat cmykValue[] = { 1, 1, 0, 0, 1}; // blue
CGColorRef colorCMYK = CGColorCreate(cmykSpace, cmykValue);
CGColorSpaceRelease(cmykSpace);
NSLog(@"colorCMYK: %@", colorCMYK);
ciColor = [CIColor colorWithCGColor:colorCMYK];
NSLog(@"cicolor: %@", ciColor); // in factï¼the color value of CIColor has converted to RGB Colorspace
NSLog(@"cicolor colorspace: %@", ciColor.colorSpace);
color = [UIColor colorWithCIColor:ciColor];
NSLog(@"UIColor with CIColor: %@", color);
NSLog(@"cicolor from UIColor: %@", color.CIColor);
NSLog(@"cicolor's colorspace: %@", color.CIColor.colorSpace);
// when UIColor init with CIColor, UIColor's CGColor will convert other colorspace to kCGColorSpaceDeviceRGB
NSLog(@"cgcolor from UIColor: %@", color.CGColor);
ããæ´ä¸ªè¿ç¨ä¸ï¼æ们éè¿è¿è¡åæ ·å¯ä»¥åç°ï¼å½æ们ç¨ä¸ä¸ªCMYKé¢è²ç©ºé´çCGColoræ¥åå§åCIColorçæ¶åï¼CIColorçé¢è²ç©ºé´ä¾ç¶æ¯CMYKï¼ä½æ¯é¢è²å¼å·²ç»è½¬æ¢æRGBçé¢è²å¼ãå½ä½¿ç¨è¯¥CIColorå建ä¸ä¸ªUIColorçæ¶åï¼æ们åéè¿CIColoråCGColorå±æ§æå°ä¿¡æ¯çæ¶åï¼æ们ä¼åç°CIColorçè²å½©ç©ºé´ä¾ç¶æ¯CMYKï¼ä½æ¯CGColoræå°æå¾å°çä¿¡æ¯è¯´æå®å·²ç»è¢«è½¬æ¢æRGB空é´äºã
äºãUIColor延伸ï¼å¦ä½å¤æ两个é¢è²æ¯å¦ç¸ç
ããåé¢æå°ä¸ç¹ï¼ä¸ç®¡UIColor使ç¨CIColorï¼CGColorè¿æ¯å ¶ä»æ¹å¼åå§åçï¼å ¶CGColorå±æ§é½æ¯å¯ç¨çãCoreGraphicsä¸æä¾ä¸ä¸ªæ¹æ³å¯ä»¥å¤æ两个CGColoræ¯å¦ç¸çï¼å æ¤æ们å¯ä»¥éè¿å¤æ两个UIColoræ¯å¦ç¸çï¼ä¸é¢æ¯çä¸ä¸ªç®åçä¾åï¼
// judge two CGColor is equal
if (CGColorEqualToColor([UIColor whiteColor].CGColor, [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor)) {
NSLog(@"The two CGColor is equal!");
}
else {
NSLog(@"The two CGColor is not equal!");
}
if (CGColorEqualToColor([UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor, [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor)) {
NSLog(@"The two CGColor is equal!");
}
else {
NSLog(@"The two CGColor is not equal!");
}
ããä¾åä¸ç¬¬ä¸é¨åæ¯å¤æ两个ç½è²çUIColoræ¯å¦ç¸çï¼è½ç¶é½æ¯ç½è²ï¼ä½æ¯é¢è²ç©ºé´æ¯ä¸ä¸æ ·çï¼éè¿è¿è¡æ们å¯ä»¥åç°ï¼æå°åºâThe
two CGColor is not
equal!âãä¾åç第äºé¨åç®åçå建äºä¸¤ä¸ªRGB空é´çUIColorï¼è¿è¡ç¨åºå¯ä»¥çåºï¼è¿ä¸¤ç§é¢è²æ¯ç¸åçã