如题所述
ããæè¿çäºçCoreGraphicsçä¸è¥¿ï¼çå°å
³äºCGColorçä¸è¥¿ï¼äºæ¯å°±æ³ç顺便ççUIColorï¼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: %@", 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); // crush2ã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ï¼è¿è¡ç¨åºå¯ä»¥çåºï¼è¿ä¸¤ç§é¢è²æ¯ç¸åçã
转载
ããä¸ã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: %@", 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); // crush2ã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ï¼è¿è¡ç¨åºå¯ä»¥çåºï¼è¿ä¸¤ç§é¢è²æ¯ç¸åçã
转载
温馨提示:答案为网友推荐,仅供参考