00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024
00025 @import "CGColor.j"
00026
00027 @import "CPCompatibility.j"
00028 @import "CPImage.j"
00029
00030
00031 var _redComponent = 0,
00032 _greenComponent = 1,
00033 _blueComponent = 2,
00034 _alphaCompnent = 3;
00035
00036 var _hueComponent = 0,
00037 _saturationComponent = 1,
00038 _brightnessComponent = 2;
00039
00040 var cachedBlackColor,
00041 cachedRedColor,
00042 cachedGreenColor,
00043 cachedBlueColor,
00044 cachedYellowColor,
00045 cachedGrayColor,
00046 cachedLightGrayColor,
00047 cachedDarkGrayColor,
00048 cachedWhiteColor,
00049 cachedBrownColor,
00050 cachedCyanColor,
00051 cachedMagentaColor,
00052 cachedOrangeColor,
00053 cachedPurpleColor,
00054 cachedShadowColor,
00055 cachedClearColor;
00056
00072 @implementation CPColor : CPObject
00073 {
00074 CPArray _components;
00075
00076 CPImage _patternImage;
00077 CPString _cssString;
00078 }
00079
00093 + (CPColor)colorWithRed:(float)red green:(float)green blue:(float)blue alpha:(float)alpha
00094 {
00095 return [[CPColor alloc] _initWithRGBA:[red, green, blue, alpha]];
00096 }
00097
00113 + (CPColor)colorWithCalibratedRed:(float)red green:(float)green blue:(float)blue alpha:(float)alpha
00114 {
00115 return [self colorWithRed:red green:green blue:blue alpha:alpha];
00116 }
00117
00118
00128 + (CPColor)colorWithWhite:(float)white alpha:(float)alpha
00129 {
00130 return [[CPColor alloc] _initWithRGBA:[white, white, white, alpha]];
00131 }
00132
00144 + (CPColor)colorWithCalibratedWhite:(float)white alpha:(float)alpha
00145 {
00146 return [self colorWithWhite:white alpha:alpha];
00147 }
00148
00158 + (CPColor)colorWithHue:(float)hue saturation:(float)saturation brightness:(float)brightness
00159 {
00160 return [self colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0];
00161 }
00162
00163 + (CPColor)colorWithHue:(float)hue saturation:(float)saturation brightness:(float)brightness alpha:(float)alpha
00164 {
00165 if(saturation === 0.0)
00166 return [CPColor colorWithCalibratedWhite:brightness / 100.0 alpha:alpha];
00167
00168 var f = hue % 60,
00169 p = (brightness * (100 - saturation)) / 10000,
00170 q = (brightness * (6000 - saturation * f)) / 600000,
00171 t = (brightness * (6000 - saturation * (60 -f))) / 600000,
00172 b = brightness / 100.0;
00173
00174 switch(FLOOR(hue / 60))
00175 {
00176 case 0: return [CPColor colorWithCalibratedRed: b green: t blue: p alpha: alpha];
00177 case 1: return [CPColor colorWithCalibratedRed: q green: b blue: p alpha: alpha];
00178 case 2: return [CPColor colorWithCalibratedRed: p green: b blue: t alpha: alpha];
00179 case 3: return [CPColor colorWithCalibratedRed: p green: q blue: b alpha: alpha];
00180 case 4: return [CPColor colorWithCalibratedRed: t green: p blue: b alpha: alpha];
00181 case 5: return [CPColor colorWithCalibratedRed: b green: p blue: q alpha: alpha];
00182 }
00183 }
00184
00195 + (CPColor)colorWithHexString:(string)hex
00196 {
00197 return [[CPColor alloc] _initWithRGBA: hexToRGB(hex)];
00198 }
00199
00203 + (CPColor)blackColor
00204 {
00205 if (!cachedBlackColor)
00206 cachedBlackColor = [[CPColor alloc] _initWithRGBA:[0.0, 0.0, 0.0, 1.0]];
00207
00208 return cachedBlackColor;
00209 }
00210
00214 + (CPColor)blueColor
00215 {
00216 if (!cachedBlueColor)
00217 cachedBlueColor = [[CPColor alloc] _initWithRGBA:[0.0, 0.0, 1.0, 1.0]];
00218
00219 return cachedBlueColor;
00220 }
00221
00225 + (CPColor)darkGrayColor
00226 {
00227 if (!cachedDarkGrayColor)
00228 cachedDarkGrayColor = [CPColor colorWithCalibratedWhite:1.0 / 3.0 alpha:1.0];
00229
00230 return cachedDarkGrayColor;
00231 }
00232
00236 + (CPColor)grayColor
00237 {
00238 if (!cachedGrayColor)
00239 cachedGrayColor = [CPColor colorWithCalibratedWhite:0.5 alpha: 1.0];
00240
00241 return cachedGrayColor;
00242 }
00243
00247 + (CPColor)greenColor
00248 {
00249 if (!cachedGreenColor)
00250 cachedGreenColor = [[CPColor alloc] _initWithRGBA:[0.0, 1.0, 0.0, 1.0]];
00251
00252 return cachedGreenColor;
00253 }
00254
00258 + (CPColor)lightGrayColor
00259 {
00260 if (!cachedLightGrayColor)
00261 cachedLightGrayColor = [CPColor colorWithCalibratedWhite:2.0 / 3.0 alpha:1.0];
00262
00263 return cachedLightGrayColor;
00264 }
00265
00269 + (CPColor)redColor
00270 {
00271 if (!cachedRedColor)
00272 cachedRedColor = [[CPColor alloc] _initWithRGBA:[1.0, 0.0, 0.0, 1.0]];
00273
00274 return cachedRedColor;
00275 }
00276
00280 + (CPColor)whiteColor
00281 {
00282 if (!cachedWhiteColor)
00283 cachedWhiteColor = [[CPColor alloc] _initWithRGBA:[1.0, 1.0, 1.0, 1.0]];
00284
00285 return cachedWhiteColor;
00286 }
00287
00291 + (CPColor)yellowColor
00292 {
00293 if (!cachedYellowColor)
00294 cachedYellowColor = [[CPColor alloc] _initWithRGBA:[1.0, 1.0, 0.0, 1.0]];
00295
00296 return cachedYellowColor;
00297 }
00298
00302 + (CPColor)brownColor
00303 {
00304 if (!cachedBrownColor)
00305 cachedBrownColor = [[CPColor alloc] _initWithRGBA:[0.6, 0.4, 0.2, 1.0]];
00306
00307 return cachedBrownColor;
00308 }
00309
00313 + (CPColor)cyanColor
00314 {
00315 if (!cachedCyanColor)
00316 cachedCyanColor = [[CPColor alloc] _initWithRGBA:[0.0, 1.0, 1.0, 1.0]];
00317
00318 return cachedCyanColor;
00319 }
00320
00324 + (CPColor)magentaColor
00325 {
00326 if (!cachedMagentaColor)
00327 cachedMagentaColor = [[CPColor alloc] _initWithRGBA:[1.0, 0.0, 1.0, 1.0]];
00328
00329 return cachedMagentaColor;
00330 }
00331
00335 + (CPColor)orangeColor
00336 {
00337 if (!cachedOrangeColor)
00338 cachedOrangeColor = [[CPColor alloc] _initWithRGBA:[1.0, 0.5, 0.0, 1.0]];
00339
00340 return cachedOrangeColor;
00341 }
00342
00346 + (CPColor)purpleColor
00347 {
00348 if (!cachedPurpleColor)
00349 cachedPurpleColor = [[CPColor alloc] _initWithRGBA:[0.5, 0.0, 0.5, 1.0]];
00350
00351 return cachedPurpleColor;
00352 }
00353
00358 + (CPColor)shadowColor
00359 {
00360 if (!cachedShadowColor)
00361 cachedShadowColor = [[CPColor alloc] _initWithRGBA:[0.0, 0.0, 0.0, 1.0 / 3.0]];
00362
00363 return cachedShadowColor;
00364 }
00365
00370 + (CPColor)clearColor
00371 {
00372 if (!cachedClearColor)
00373 cachedClearColor = [self colorWithCalibratedWhite:0.0 alpha:0.0];
00374
00375 return cachedClearColor;
00376 }
00377
00378 + (CPColor)alternateSelectedControlColor
00379 {
00380 return [[CPColor alloc] _initWithRGBA:[0.22, 0.46, 0.84, 1.0]];
00381 }
00382
00383 + (CPColor)secondarySelectedControlColor
00384 {
00385 return [[CPColor alloc] _initWithRGBA:[0.83, 0.83, 0.83, 1.0]];
00386 }
00387
00393 + (CPColor)colorWithPatternImage:(CPImage)anImage
00394 {
00395 return [[CPColor alloc] _initWithPatternImage:anImage];
00396 }
00397
00404 + (CPColor)colorWithCSSString:(CPString)aString
00405 {
00406 return [[CPColor alloc] _initWithCSSString: aString];
00407 }
00408
00409
00410 - (id)_initWithCSSString:(CPString)aString
00411 {
00412 if(aString.indexOf("rgb") == CPNotFound)
00413 return nil;
00414
00415 self = [super init];
00416
00417 var startingIndex = aString.indexOf("(");
00418 var parts = aString.substring(startingIndex+1).split(',');
00419
00420 _components = [
00421 parseInt(parts[0], 10) / 255.0,
00422 parseInt(parts[1], 10) / 255.0,
00423 parseInt(parts[2], 10) / 255.0,
00424 parts[3] ? parseInt(parts[3], 10) / 255.0 : 1.0
00425 ]
00426
00427 _cssString = aString;
00428
00429 return self;
00430 }
00431
00432
00433 - (id)_initWithRGBA:(CPArray)components
00434 {
00435 self = [super init];
00436
00437 if (self)
00438 {
00439 _components = components;
00440
00441 if (!CPFeatureIsCompatible(CPCSSRGBAFeature)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x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x17,0x89,0x99,0x55,0x0,0x0,0x0,0x1,0x74,0x52,0x4e,0x53,0x0,0x40,0xe6,0xd8,0x66,0x0,0x0,0x0,0x10,0x49,0x44,0x41,0x54,0x78,0xda,0x62,0x60,0x0,0x0,0x0,0x0,0xff,0xff,0x3,0x0,0x0,0x2,0x0,0x1,0x24,0x7f,0x24,0xf1,0x0,0x0,0x0,0x0,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,0xff];
00444 var r_off = 41;
00445 var g_off = 42;
00446 var b_off = 43;
00447 var a_off = 821;
00448 var plte_crc_off = 809;
00449 var trns_crc_off = 822;
00450 var plte_type_off = 37;
00451 var trns_type_off = 817;
00452
00453 bytes[r_off] = Math.round(_components[0]*255);
00454 bytes[g_off] = Math.round(_components[1]*255);
00455 bytes[b_off] = Math.round(_components[2]*255);
00456 bytes[a_off] = Math.round(_components[3]*255);
00457
00458
00459 var new_plte_crc = integerToBytes(CRC32.getCRC(bytes, plte_type_off, 4+768), 4);
00460 var new_trns_crc = integerToBytes(CRC32.getCRC(bytes, trns_type_off, 4+1), 4);
00461
00462
00463 for (var i = 0; i < 4; i++)
00464 {
00465 bytes[plte_crc_off+i] = new_plte_crc[i];
00466 bytes[trns_crc_off+i] = new_trns_crc[i];
00467 }
00468
00469
00470 var base64image = Base64.encode(bytes);
00471
00472 _cssString = "url(\"data:image/png;base64," + base64image + "\")";
00473 }
00474 else
00475 {
00476 var hasAlpha = CPFeatureIsCompatible(CPCSSRGBAFeature) && _components[3] != 1.0;
00477
00478 _cssString = (hasAlpha ? "rgba(" : "rgb(") +
00479 parseInt(_components[0] * 255.0) + ", " +
00480 parseInt(_components[1] * 255.0) + ", " +
00481 parseInt(_components[2] * 255.0) +
00482 (hasAlpha ? (", " + _components[3]) : "") + ")";
00483 }
00484 }
00485 return self;
00486 }
00487
00488
00489 - (id)_initWithPatternImage:(CPImage)anImage
00490 {
00491 self = [super init];
00492
00493 if (self)
00494 {
00495 _patternImage = anImage;
00496 _cssString = "url(\"" + [_patternImage filename] + "\")";
00497 }
00498
00499 return self;
00500 }
00501
00505 - (CPImage)patternImage
00506 {
00507 return _patternImage;
00508 }
00509
00513 - (float)alphaComponent
00514 {
00515 return _components[3];
00516 }
00517
00521 - (float)blueComponent
00522 {
00523 return _components[2];
00524 }
00525
00529 - (float)greenComponent
00530 {
00531 return _components[1];
00532 }
00533
00537 - (float)redComponent
00538 {
00539 return _components[0];
00540 }
00541
00553 - (CPArray)components
00554 {
00555 return _components;
00556 }
00557
00565 - (CPColor)colorWithAlphaComponent:(float)anAlphaComponent
00566 {
00567 var components = _components.slice();
00568
00569 components[components.length - 1] = anAlphaComponent;
00570
00571 return [[[self class] alloc] _initWithRGBA:components];
00572 }
00573
00584 - (CPArray)hsbComponents
00585 {
00586 var red = ROUND(_components[_redComponent] * 255.0),
00587 green = ROUND(_components[_greenComponent] * 255.0),
00588 blue = ROUND(_components[_blueComponent] * 255.0);
00589
00590 var max = MAX(red, green, blue),
00591 min = MIN(red, green, blue),
00592 delta = max - min;
00593
00594 var brightness = max / 255.0,
00595 saturation = (max != 0) ? delta / max : 0;
00596
00597 var hue;
00598 if(saturation == 0)
00599 hue = 0;
00600 else
00601 {
00602 var rr = (max - red) / delta;
00603 var gr = (max - green) / delta;
00604 var br = (max - blue) / delta;
00605
00606 if (red == max)
00607 hue = br - gr;
00608 else if (green == max)
00609 hue = 2 + rr - br;
00610 else
00611 hue = 4 + gr - rr;
00612
00613 hue /= 6;
00614 if (hue < 0)
00615 hue++;
00616 }
00617
00618 return [
00619 ROUND(hue * 360.0),
00620 ROUND(saturation * 100.0),
00621 ROUND(brightness * 100.0)
00622 ];
00623 }
00624
00634 - (CPString)cssString
00635 {
00636 return _cssString;
00637 }
00638
00642 - (CPString)hexString
00643 {
00644 return rgbToHex([self redComponent], [self greenComponent], [self blueComponent])
00645 }
00646
00647 - (BOOL)isEqual:(CPColor)aColor
00648 {
00649 if (!aColor)
00650 return NO;
00651
00652 if (aColor === self)
00653 return YES;
00654
00655 return [aColor isKindOfClass:CPColor] && [aColor cssString] === [self cssString];
00656 }
00657
00658 - (CPString)description
00659 {
00660 return [super description]+" "+[self cssString];
00661 }
00662
00663 @end
00664
00665 @implementation CPColor (CoreGraphicsExtensions)
00666
00670 - (void)set
00671 {
00672 [self setFill];
00673 [self setStroke];
00674 }
00675
00679 - (void)setFill
00680 {
00681 var ctx = [[CPGraphicsContext currentContext] graphicsPort];
00682 CGContextSetFillColor(ctx, self);
00683 }
00684
00688 - (void)setStroke
00689 {
00690 var ctx = [[CPGraphicsContext currentContext] graphicsPort];
00691 CGContextSetStrokeColor(ctx, self);
00692 }
00693
00694 @end
00695
00696 @implementation CPColor (Debugging)
00697
00698 + (CPColor)randomColor
00699 {
00700 return [CPColor colorWithRed:RAND() green:RAND() blue:RAND() alpha:1.0];
00701 }
00702
00703 @end
00704
00705 var CPColorComponentsKey = @"CPColorComponentsKey",
00706 CPColorPatternImageKey = @"CPColorPatternImageKey";
00707
00708 @implementation CPColor (CPCoding)
00709
00714 - (id)initWithCoder:(CPCoder)aCoder
00715 {
00716 if ([aCoder containsValueForKey:CPColorPatternImageKey])
00717 return [self _initWithPatternImage:[aCoder decodeObjectForKey:CPColorPatternImageKey]];
00718
00719 return [self _initWithRGBA:[aCoder decodeObjectForKey:CPColorComponentsKey]];
00720 }
00721
00726 - (void)encodeWithCoder:(CPCoder)aCoder
00727 {
00728 if (_patternImage)
00729 [aCoder encodeObject:_patternImage forKey:CPColorPatternImageKey];
00730 else
00731 [aCoder encodeObject:_components forKey:CPColorComponentsKey];
00732 }
00733
00734 @end
00735
00736 var hexCharacters = "0123456789ABCDEF";
00737
00738
00739 var hexToRGB, integerToBytes, rgbToHex, byteToHex;
00740
00747 function hexToRGB(hex)
00748 {
00749 if ( hex.length == 3 )
00750 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
00751 if(hex.length != 6)
00752 return null;
00753
00754 hex = hex.toUpperCase();
00755
00756 for(var i=0; i<hex.length; i++)
00757 if(hexCharacters.indexOf(hex.charAt(i)) == -1)
00758 return null;
00759
00760 var red = (hexCharacters.indexOf(hex.charAt(0)) * 16 + hexCharacters.indexOf(hex.charAt(1))) / 255.0;
00761 var green = (hexCharacters.indexOf(hex.charAt(2)) * 16 + hexCharacters.indexOf(hex.charAt(3))) / 255.0;
00762 var blue = (hexCharacters.indexOf(hex.charAt(4)) * 16 + hexCharacters.indexOf(hex.charAt(5))) / 255.0;
00763
00764 return [red, green, blue, 1.0];
00765 }
00766
00767 function integerToBytes(integer, length) {
00768 if (!length)
00769 length = (integer == 0) ? 1 : Math.round((Math.log(integer)/Math.log(2))/8+0.5);
00770
00771 var bytes = new Array(length);
00772 for (var i = length-1; i >= 0; i--) {
00773 bytes[i] = integer & 255;
00774 integer = integer >> 8
00775 }
00776 return bytes;
00777 }
00778
00779 function rgbToHex(r,g,b) {
00780 return byteToHex(r) + byteToHex(g) + byteToHex(b);
00781 }
00782
00783 function byteToHex(n) {
00784 if (!n || isNaN(n)) return "00";
00785 n = ROUND(MIN(255,MAX(0,256*n)));
00786 return hexCharacters.charAt((n - n % 16) / 16) +
00787 hexCharacters.charAt(n % 16);
00788 }
00789
00790
00791
00792
00793
00794