4 // Created by Chad Weider on 12/3/05.
5 // Copyright (c) 2006 Cotingent.
6 // Some rights reserved: <http://creativecommons.org/licenses/by/2.5/>
10 #import "CTGradient.h"
12 @interface CTGradient (Private)
14 - (void)setBlendingMode:(CTGradientBlendingMode)mode;
15 - (void)addElement:(CTGradientElement*)newElement;
17 - (CTGradientElement *)elementAtIndex:(unsigned)index;
19 - (CTGradientElement)removeElementAtIndex:(unsigned)index;
20 - (CTGradientElement)removeElementAtPosition:(float)position;
23 //C Fuctions for color blending
24 void linearEvaluation (void *info, const float *in, float *out);
25 void chromaticEvaluation(void *info, const float *in, float *out);
26 void inverseChromaticEvaluation(void *info, const float *in, float *out);
27 void transformRGB_HSV(float *components);
28 void transformHSV_RGB(float *components);
29 void resolveHSV(float *color1, float *color2);
32 @implementation CTGradient
33 /////////////////////////////////////Initialization Type Stuff
41 [self setBlendingMode:CTLinearBlendingMode];
53 CGFunctionRelease(gradientFunction);
55 CTGradientElement *elementToRemove = elementList;
56 while(elementList != nil)
58 elementToRemove = elementList;
59 elementList = elementList->nextElement;
60 free(elementToRemove);
66 - (id)copyWithZone:(NSZone *)zone
68 CTGradient *copy = [[[self class] allocWithZone:zone] init];
70 //now just copy my elementlist
71 CTGradientElement *currentElement = elementList;
72 while(currentElement != nil)
74 [copy addElement:currentElement];
75 currentElement = currentElement->nextElement;
78 [copy setBlendingMode:blendingMode];
83 - (void)encodeWithCoder:(NSCoder *)coder
85 if([coder allowsKeyedCoding])
88 CTGradientElement *currentElement = elementList;
89 while(currentElement != nil)
91 [coder encodeValueOfObjCType:@encode(float) at:&(currentElement->red)];
92 [coder encodeValueOfObjCType:@encode(float) at:&(currentElement->green)];
93 [coder encodeValueOfObjCType:@encode(float) at:&(currentElement->blue)];
94 [coder encodeValueOfObjCType:@encode(float) at:&(currentElement->alpha)];
95 [coder encodeValueOfObjCType:@encode(float) at:&(currentElement->position)];
98 currentElement = currentElement->nextElement;
100 [coder encodeInt:count forKey:@"CTGradientElementCount"];
101 [coder encodeInt:blendingMode forKey:@"CTGradientBlendingMode"];
104 [NSException raise:NSInvalidArchiveOperationException format:@"Only supports NSKeyedArchiver coders"];
107 - (id)initWithCoder:(NSCoder *)coder
111 [self setBlendingMode:[coder decodeIntForKey:@"CTGradientBlendingMode"]];
112 unsigned count = [coder decodeIntForKey:@"CTGradientElementCount"];
116 CTGradientElement newElement;
118 [coder decodeValueOfObjCType:@encode(float) at:&(newElement.red)];
119 [coder decodeValueOfObjCType:@encode(float) at:&(newElement.green)];
120 [coder decodeValueOfObjCType:@encode(float) at:&(newElement.blue)];
121 [coder decodeValueOfObjCType:@encode(float) at:&(newElement.alpha)];
122 [coder decodeValueOfObjCType:@encode(float) at:&(newElement.position)];
125 [self addElement:&newElement];
135 #pragma mark Creation
136 + (id)gradientWithBeginningColor:(NSColor *)begin endingColor:(NSColor *)end
138 id newInstance = [[[self class] alloc] init];
140 CTGradientElement color1;
141 CTGradientElement color2;
143 [[begin colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&color1.red
146 alpha:&color1.alpha];
148 [[end colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&color2.red
151 alpha:&color2.alpha];
155 [newInstance addElement:&color1];
156 [newInstance addElement:&color2];
158 return [newInstance autorelease];
161 + (id)aquaSelectedGradient
163 id newInstance = [[[self class] alloc] init];
165 CTGradientElement color1;
172 CTGradientElement color2;
177 color2.position = 11.5/23;
179 CTGradientElement color3;
184 color3.position = 11.5/23;
186 CTGradientElement color4;
193 [newInstance addElement:&color1];
194 [newInstance addElement:&color2];
195 [newInstance addElement:&color3];
196 [newInstance addElement:&color4];
198 return [newInstance autorelease];
201 + (id)aquaNormalGradient
203 id newInstance = [[[self class] alloc] init];
205 CTGradientElement color1;
206 color1.red = color1.green = color1.blue = 0.95;
210 CTGradientElement color2;
211 color2.red = color2.green = color2.blue = 0.83;
213 color2.position = 11.5/23;
215 CTGradientElement color3;
216 color3.red = color3.green = color3.blue = 0.95;
218 color3.position = 11.5/23;
220 CTGradientElement color4;
221 color4.red = color4.green = color4.blue = 0.92;
225 [newInstance addElement:&color1];
226 [newInstance addElement:&color2];
227 [newInstance addElement:&color3];
228 [newInstance addElement:&color4];
230 return [newInstance autorelease];
233 + (id)aquaPressedGradient
235 id newInstance = [[[self class] alloc] init];
237 CTGradientElement color1;
238 color1.red = color1.green = color1.blue = 0.80;
242 CTGradientElement color2;
243 color2.red = color2.green = color2.blue = 0.64;
245 color2.position = 11.5/23;
247 CTGradientElement color3;
248 color3.red = color3.green = color3.blue = 0.80;
250 color3.position = 11.5/23;
252 CTGradientElement color4;
253 color4.red = color4.green = color4.blue = 0.77;
257 [newInstance addElement:&color1];
258 [newInstance addElement:&color2];
259 [newInstance addElement:&color3];
260 [newInstance addElement:&color4];
262 return [newInstance autorelease];
265 + (id)unifiedSelectedGradient
267 id newInstance = [[[self class] alloc] init];
269 CTGradientElement color1;
270 color1.red = color1.green = color1.blue = 0.85;
274 CTGradientElement color2;
275 color2.red = color2.green = color2.blue = 0.95;
279 [newInstance addElement:&color1];
280 [newInstance addElement:&color2];
282 return [newInstance autorelease];
285 + (id)unifiedNormalGradient
287 id newInstance = [[[self class] alloc] init];
289 CTGradientElement color1;
290 color1.red = color1.green = color1.blue = 0.75;
294 CTGradientElement color2;
295 color2.red = color2.green = color2.blue = 0.90;
299 [newInstance addElement:&color1];
300 [newInstance addElement:&color2];
302 return [newInstance autorelease];
305 + (id)unifiedPressedGradient
307 id newInstance = [[[self class] alloc] init];
309 CTGradientElement color1;
310 color1.red = color1.green = color1.blue = 0.60;
314 CTGradientElement color2;
315 color2.red = color2.green = color2.blue = 0.75;
319 [newInstance addElement:&color1];
320 [newInstance addElement:&color2];
322 return [newInstance autorelease];
325 + (id)unifiedDarkGradient
327 id newInstance = [[[self class] alloc] init];
329 CTGradientElement color1;
330 color1.red = color1.green = color1.blue = 0.68;
334 CTGradientElement color2;
335 color2.red = color2.green = color2.blue = 0.83;
339 [newInstance addElement:&color1];
340 [newInstance addElement:&color2];
342 return [newInstance autorelease];
345 + (id)sourceListSelectedGradient
347 id newInstance = [[[self class] alloc] init];
349 CTGradientElement color1;
356 CTGradientElement color2;
363 [newInstance addElement:&color1];
364 [newInstance addElement:&color2];
366 return [newInstance autorelease];
369 + (id)sourceListUnselectedGradient
371 id newInstance = [[[self class] alloc] init];
373 CTGradientElement color1;
380 CTGradientElement color2;
387 [newInstance addElement:&color1];
388 [newInstance addElement:&color2];
390 return [newInstance autorelease];
393 + (id)rainbowGradient
395 id newInstance = [[[self class] alloc] init];
397 CTGradientElement color1;
402 color1.position = 0.0;
404 CTGradientElement color2;
409 color2.position = 1.0;
411 [newInstance addElement:&color1];
412 [newInstance addElement:&color2];
414 [newInstance setBlendingMode:CTChromaticBlendingMode];
416 return [newInstance autorelease];
419 + (id)hydrogenSpectrumGradient
421 id newInstance = [[[self class] alloc] init];
423 struct {float hue; float position; float width;} colorBands[4];
425 colorBands[0].hue = 22;
426 colorBands[0].position = .145;
427 colorBands[0].width = .01;
429 colorBands[1].hue = 200;
430 colorBands[1].position = .71;
431 colorBands[1].width = .008;
433 colorBands[2].hue = 253;
434 colorBands[2].position = .885;
435 colorBands[2].width = .005;
437 colorBands[3].hue = 275;
438 colorBands[3].position = .965;
439 colorBands[3].width = .003;
442 /////////////////////////////
443 for(i = 0; i < 4; i++)
446 color[0] = colorBands[i].hue - 180*colorBands[i].width;
450 transformHSV_RGB(color);
451 CTGradientElement fadeIn;
452 fadeIn.red = color[0];
453 fadeIn.green = color[1];
454 fadeIn.blue = color[2];
455 fadeIn.alpha = color[3];
456 fadeIn.position = colorBands[i].position - colorBands[i].width;
459 color[0] = colorBands[i].hue;
463 transformHSV_RGB(color);
464 CTGradientElement band;
466 band.green = color[1];
467 band.blue = color[2];
468 band.alpha = color[3];
469 band.position = colorBands[i].position;
471 color[0] = colorBands[i].hue + 180*colorBands[i].width;
475 transformHSV_RGB(color);
476 CTGradientElement fadeOut;
477 fadeOut.red = color[0];
478 fadeOut.green = color[1];
479 fadeOut.blue = color[2];
480 fadeOut.alpha = color[3];
481 fadeOut.position = colorBands[i].position + colorBands[i].width;
484 [newInstance addElement:&fadeIn];
485 [newInstance addElement:&band];
486 [newInstance addElement:&fadeOut];
489 [newInstance setBlendingMode:CTChromaticBlendingMode];
491 return [newInstance autorelease];
498 #pragma mark Modification
499 - (CTGradient *)gradientWithAlphaComponent:(float)alpha
501 id newInstance = [[[self class] alloc] init];
503 CTGradientElement *curElement = elementList;
504 CTGradientElement tempElement;
506 while(curElement != nil)
508 tempElement = *curElement;
509 tempElement.alpha = alpha;
510 [newInstance addElement:&tempElement];
512 curElement = curElement->nextElement;
515 return [newInstance autorelease];
518 - (CTGradient *)gradientWithBlendingMode:(CTGradientBlendingMode)mode
520 CTGradient *newGradient = [self copy];
522 [newGradient setBlendingMode:mode];
524 return [newGradient autorelease];
528 //Adds a color stop with <color> at <position> in elementList
529 //(if two elements are at the same position then added imediatly after the one that was there already)
530 - (CTGradient *)addColorStop:(NSColor *)color atPosition:(float)position
532 CTGradient *newGradient = [self copy];
533 CTGradientElement newGradientElement;
535 //put the components of color into the newGradientElement - must make sure it is a RGB color (not Gray or CMYK)
536 [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&newGradientElement.red
537 green:&newGradientElement.green
538 blue:&newGradientElement.blue
539 alpha:&newGradientElement.alpha];
540 newGradientElement.position = position;
542 //Pass it off to addElement to take care of adding it to the elementList
543 [newGradient addElement:&newGradientElement];
545 return [newGradient autorelease];
549 //Removes the color stop at <position> from elementList
550 - (CTGradient *)removeColorStopAtPosition:(float)position
552 CTGradient *newGradient = [self copy];
553 CTGradientElement removedElement = [newGradient removeElementAtPosition:position];
555 if(isnan(removedElement.position))
556 [NSException raise:NSRangeException format:@"-[%@ removeColorStopAtPosition:]: no such colorStop at position (%f)", [self class], position];
558 return [newGradient autorelease];
561 - (CTGradient *)removeColorStopAtIndex:(unsigned)index
563 CTGradient *newGradient = [self copy];
564 CTGradientElement removedElement = [newGradient removeElementAtIndex:index];
566 if(isnan(removedElement.position))
567 [NSException raise:NSRangeException format:@"-[%@ removeColorStopAtIndex:]: index (%i) beyond bounds", [self class], index];
569 return [newGradient autorelease];
575 #pragma mark Information
576 - (CTGradientBlendingMode)blendingMode
581 //Returns color at <position> in gradient
582 - (NSColor *)colorStopAtIndex:(unsigned)index
584 CTGradientElement *element = [self elementAtIndex:index];
587 return [NSColor colorWithCalibratedRed:element->red
590 alpha:element->alpha];
592 [NSException raise:NSRangeException format:@"-[%@ removeColorStopAtIndex:]: index (%i) beyond bounds", [self class], index];
597 - (NSColor *)colorAtPosition:(float)position
603 case CTLinearBlendingMode:
604 linearEvaluation(&elementList, &position, components); break;
605 case CTChromaticBlendingMode:
606 chromaticEvaluation(&elementList, &position, components); break;
607 case CTInverseChromaticBlendingMode:
608 inverseChromaticEvaluation(&elementList, &position, components); break;
612 return [NSColor colorWithCalibratedRed:components[0]
615 alpha:components[3]];
622 - (void)drawSwatchInRect:(NSRect)rect
624 [self fillRect:rect angle:45];
627 - (void)fillRect:(NSRect)rect angle:(float)angle
629 //First Calculate where the beginning and ending points should be
633 if(angle == 0) //screw the calculations - we know the answer
635 startPoint = CGPointMake(NSMinX(rect), NSMinY(rect)); //right of rect
636 endPoint = CGPointMake(NSMaxX(rect), NSMinY(rect)); //left of rect
638 else if(angle == 90) //same as above
640 startPoint = CGPointMake(NSMinX(rect), NSMinY(rect)); //bottom of rect
641 endPoint = CGPointMake(NSMinX(rect), NSMaxY(rect)); //top of rect
643 else //ok, we'll do the calculations now
646 float sina, cosa, tana;
652 float rangle = angle * pi/180; //convert the angle to radians
654 if(fabsf(tan(rangle))<=1) //for range [-45,45], [135,225]
663 length = x/fabsf(cosa)+(y-x*fabsf(tana))*fabsf(sina);
665 deltax = length*cosa/2;
666 deltay = length*sina/2;
668 else //for range [45,135], [225,315]
673 sina = sin(rangle - 90*pi/180);
674 cosa = cos(rangle - 90*pi/180);
675 tana = tan(rangle - 90*pi/180);
677 length = x/fabsf(cosa)+(y-x*fabsf(tana))*fabsf(sina);
679 deltax =-length*sina/2;
680 deltay = length*cosa/2;
683 startPoint = CGPointMake(NSMidX(rect)-deltax, NSMidY(rect)-deltay);
684 endPoint = CGPointMake(NSMidX(rect)+deltax, NSMidY(rect)+deltay);
687 //Calls to CoreGraphics
688 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
689 CGContextSaveGState(currentContext);
690 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
691 CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
693 CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
696 CGShadingRef myCGShading = CGShadingCreateAxial(colorspace, startPoint, endPoint, gradientFunction, false, false);
698 CGContextClipToRect(currentContext , *(CGRect *)&rect); //This is where the action happens
699 CGContextDrawShading(currentContext, myCGShading);
701 CGShadingRelease (myCGShading);
702 CGColorSpaceRelease(colorspace );
703 CGContextRestoreGState(currentContext);
706 - (void)radialFillRect:(NSRect)rect
708 CGPoint startPoint , endPoint;
709 float startRadius, endRadius;
711 startPoint = endPoint = CGPointMake(NSMidX(rect), NSMidY(rect));
715 if(NSHeight(rect)>NSWidth(rect))
716 endRadius = NSHeight(rect)/2;
718 endRadius = NSWidth(rect)/2;
720 //Calls to CoreGraphics
721 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
722 CGContextSaveGState(currentContext);
723 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
724 CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
726 CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
729 CGShadingRef myCGShading = CGShadingCreateRadial(colorspace, startPoint, startRadius, endPoint, endRadius, gradientFunction, true, true);
731 CGContextClipToRect (currentContext , *(CGRect *)&rect);
732 CGContextDrawShading(currentContext , myCGShading); //This is where the action happens
734 CGShadingRelease (myCGShading);
735 CGColorSpaceRelease (colorspace);
736 CGContextRestoreGState(currentContext);
743 #pragma mark Private Methods
744 - (void)setBlendingMode:(CTGradientBlendingMode)mode;
748 //Choose what blending function to use
749 void *evaluationFunction = NULL;
752 case CTLinearBlendingMode:
753 evaluationFunction = &linearEvaluation; break;
754 case CTChromaticBlendingMode:
755 evaluationFunction = &chromaticEvaluation; break;
756 case CTInverseChromaticBlendingMode:
757 evaluationFunction = &inverseChromaticEvaluation; break;
760 //replace the current CoreGraphics Function with new one
761 if(gradientFunction != NULL)
762 CGFunctionRelease(gradientFunction);
764 CGFunctionCallbacks evaluationCallbackInfo = {0 , evaluationFunction, NULL}; //Version, evaluator function, cleanup function
766 static const float input_value_range [2] = { 0, 1 }; //range for the evaluator input
767 static const float output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 }; //ranges for the evaluator output (4 returned values)
769 gradientFunction = CGFunctionCreate(&elementList, //the two transition colors
770 1, input_value_range , //number of inputs (just fraction of progression)
771 4, output_value_ranges, //number of outputs (4 - RGBa)
772 &evaluationCallbackInfo); //info for using the evaluator function
775 - (void)addElement:(CTGradientElement *)newElement
777 if(elementList == nil || newElement->position < elementList->position) //inserting at beginning of list
779 CTGradientElement *tmpNext = elementList;
780 elementList = malloc(sizeof(CTGradientElement));
781 *elementList = *newElement;
782 elementList->nextElement = tmpNext;
784 else //inserting somewhere inside list
786 CTGradientElement *curElement = elementList;
788 while(curElement->nextElement != nil && !((curElement->position <= newElement->position) && (newElement->position < curElement->nextElement->position)))
790 curElement = curElement->nextElement;
793 CTGradientElement *tmpNext = curElement->nextElement;
794 curElement->nextElement = malloc(sizeof(CTGradientElement));
795 *(curElement->nextElement) = *newElement;
796 curElement->nextElement->nextElement = tmpNext;
800 - (CTGradientElement)removeElementAtIndex:(unsigned)index
802 CTGradientElement removedElement;
804 if(elementList != nil)
808 CTGradientElement *tmpNext = elementList;
809 elementList = elementList->nextElement;
811 removedElement = *tmpNext;
814 return removedElement;
817 unsigned count = 1; //we want to start one ahead
818 CTGradientElement *currentElement = elementList;
819 while(currentElement->nextElement != nil)
823 CTGradientElement *tmpNext = currentElement->nextElement;
824 currentElement->nextElement = currentElement->nextElement->nextElement;
826 removedElement = *tmpNext;
829 return removedElement;
833 currentElement = currentElement->nextElement;
837 //element is not found, return empty element
838 removedElement.red = 0.0;
839 removedElement.green = 0.0;
840 removedElement.blue = 0.0;
841 removedElement.alpha = 0.0;
842 removedElement.position = NAN;
843 removedElement.nextElement = nil;
845 return removedElement;
848 - (CTGradientElement)removeElementAtPosition:(float)position
850 CTGradientElement removedElement;
852 if(elementList != nil)
854 if(elementList->position == position)
856 CTGradientElement *tmpNext = elementList;
857 elementList = elementList->nextElement;
859 removedElement = *tmpNext;
862 return removedElement;
866 CTGradientElement *curElement = elementList;
867 while(curElement->nextElement != nil)
869 if(curElement->nextElement->position == position)
871 CTGradientElement *tmpNext = curElement->nextElement;
872 curElement->nextElement = curElement->nextElement->nextElement;
874 removedElement = *tmpNext;
877 return removedElement;
883 //element is not found, return empty element
884 removedElement.red = 0.0;
885 removedElement.green = 0.0;
886 removedElement.blue = 0.0;
887 removedElement.alpha = 0.0;
888 removedElement.position = NAN;
889 removedElement.nextElement = nil;
891 return removedElement;
895 - (CTGradientElement *)elementAtIndex:(unsigned)index;
898 CTGradientElement *currentElement = elementList;
900 while(currentElement != nil)
903 return currentElement;
906 currentElement = currentElement->nextElement;
915 #pragma mark Core Graphics
916 //////////////////////////////////////Blending Functions/////////////////////////////////////
917 void linearEvaluation (void *info, const float *in, float *out)
919 float position = *in;
921 if(*(CTGradientElement **)info == nil) //if elementList is empty return clear color
923 out[0] = out[1] = out[2] = out[3] = 1;
927 //This grabs the first two colors in the sequence
928 CTGradientElement *color1 = *(CTGradientElement **)info;
929 CTGradientElement *color2 = color1->nextElement;
931 //make sure first color and second color are on other sides of position
932 while(color2 != nil && color2->position < position)
935 color2 = color1->nextElement;
937 //if we don't have another color then make next color the same color
943 //----------FailSafe settings----------
944 //color1->red = 1; color2->red = 0;
945 //color1->green = 1; color2->green = 0;
946 //color1->blue = 1; color2->blue = 0;
947 //color1->alpha = 1; color2->alpha = 1;
948 //color1->position = .5;
949 //color2->position = .5;
950 //-------------------------------------
952 if(position <= color1->position) //Make all below color color1's position equal to color1
954 out[0] = color1->red;
955 out[1] = color1->green;
956 out[2] = color1->blue;
957 out[3] = color1->alpha;
959 else if (position >= color2->position) //Make all above color color2's position equal to color2
961 out[0] = color2->red;
962 out[1] = color2->green;
963 out[2] = color2->blue;
964 out[3] = color2->alpha;
966 else //Interpolate color at postions between color1 and color1
968 //adjust position so that it goes from 0 to 1 in the range from color 1 & 2's position
969 position = (position-color1->position)/(color2->position - color1->position);
971 out[0] = (color2->red - color1->red )*position + color1->red;
972 out[1] = (color2->green - color1->green)*position + color1->green;
973 out[2] = (color2->blue - color1->blue )*position + color1->blue;
974 out[3] = (color2->alpha - color1->alpha)*position + color1->alpha;
981 //Chromatic Evaluation -
982 // This blends colors by their Hue, Saturation, and Value(Brightness) right now I just
983 // transform the RGB values stored in the CTGradientElements to HSB, in the future I may
984 // streamline it to avoid transforming in and out of HSB colorspace *for later*
986 // For the chromatic blend we shift the hue of color1 to meet the hue of color2. To do
987 // this we will add to the hue's angle (if we subtract we'll be doing the inverse
988 // chromatic...scroll down more for that). All we need to do is keep adding to the hue
989 // until we wrap around the colorwheel and get to color2.
990 void chromaticEvaluation(void *info, const float *in, float *out)
992 float position = *in;
994 if(*(CTGradientElement **)info == nil) //if elementList is empty return clear color
996 out[0] = out[1] = out[2] = out[3] = 1;
1000 //This grabs the first two colors in the sequence
1001 CTGradientElement *color1 = *(CTGradientElement **)info;
1002 CTGradientElement *color2 = color1->nextElement;
1007 //make sure first color and second color are on other sides of position
1008 while(color2 != nil && color2->position < position)
1011 color2 = color1->nextElement;
1013 //if we don't have another color then make next color the same color
1020 c1[0] = color1->red;
1021 c1[1] = color1->green;
1022 c1[2] = color1->blue;
1023 c1[3] = color1->alpha;
1025 c2[0] = color2->red;
1026 c2[1] = color2->green;
1027 c2[2] = color2->blue;
1028 c2[3] = color2->alpha;
1030 transformRGB_HSV(c1);
1031 transformRGB_HSV(c2);
1034 if(c1[0] > c2[0]) //if color1's hue is higher than color2's hue then
1035 c2[0] += 360; // we need to move c2 one revolution around the wheel
1038 if(position <= color1->position) //Make all below color color1's position equal to color1
1045 else if (position >= color2->position) //Make all above color color2's position equal to color2
1052 else //Interpolate color at postions between color1 and color1
1054 //adjust position so that it goes from 0 to 1 in the range from color 1 & 2's position
1055 position = (position-color1->position)/(color2->position - color1->position);
1057 out[0] = (c2[0] - c1[0])*position + c1[0];
1058 out[1] = (c2[1] - c1[1])*position + c1[1];
1059 out[2] = (c2[2] - c1[2])*position + c1[2];
1060 out[3] = (c2[3] - c1[3])*position + c1[3];
1063 transformHSV_RGB(out);
1065 //if(position > -1 && out[0] == out[1] && out[1] == out[2] && out[0]==0)
1066 //printf("%.4f: %.4f,%.4f,%.4f\n",position,out[0],out[1],out[2]);
1067 //printf("%.4f: %.4f,%.4f,%.4f\n",position,color1->red,color1->green,color1->blue);
1072 //Inverse Chromatic Evaluation -
1073 // Inverse Chromatic is about the same story as Chromatic Blend, but here the Hue
1074 // is strictly decreasing, that is we need to get from color1 to color2 by decreasing
1075 // the 'angle' (i.e. 90º -> 180º would be done by subtracting 270º and getting -180º...
1076 // which is equivalent to 180º mod 360º
1077 void inverseChromaticEvaluation(void *info, const float *in, float *out)
1079 float position = *in;
1081 if(*(CTGradientElement **)info == nil) //if elementList is empty return clear color
1083 out[0] = out[1] = out[2] = out[3] = 1;
1087 //This grabs the first two colors in the sequence
1088 CTGradientElement *color1 = *(CTGradientElement **)info;
1089 CTGradientElement *color2 = color1->nextElement;
1094 //make sure first color and second color are on other sides of position
1095 while(color2 != nil && color2->position < position)
1098 color2 = color1->nextElement;
1100 //if we don't have another color then make next color the same color
1106 c1[0] = color1->red;
1107 c1[1] = color1->green;
1108 c1[2] = color1->blue;
1109 c1[3] = color1->alpha;
1111 c2[0] = color2->red;
1112 c2[1] = color2->green;
1113 c2[2] = color2->blue;
1114 c2[3] = color2->alpha;
1116 transformRGB_HSV(c1);
1117 transformRGB_HSV(c2);
1120 if(c1[0] < c2[0]) //if color1's hue is higher than color2's hue then
1121 c1[0] += 360; // we need to move c2 one revolution back on the wheel
1124 if(position <= color1->position) //Make all below color color1's position equal to color1
1131 else if (position >= color2->position) //Make all above color color2's position equal to color2
1138 else //Interpolate color at postions between color1 and color1
1140 //adjust position so that it goes from 0 to 1 in the range from color 1 & 2's position
1141 position = (position-color1->position)/(color2->position - color1->position);
1143 out[0] = (c2[0] - c1[0])*position + c1[0];
1144 out[1] = (c2[1] - c1[1])*position + c1[1];
1145 out[2] = (c2[2] - c1[2])*position + c1[2];
1146 out[3] = (c2[3] - c1[3])*position + c1[3];
1149 transformHSV_RGB(out);
1161 void transformRGB_HSV(float *components) //H,S,B -> R,G,B
1163 float H = 0.0, S = 0.0, V = 0.0;
1164 float R = components[0],
1168 float MAX = R > G ? (R > B ? R : B) : (G > B ? G : B),
1169 MIN = R < G ? (R < B ? R : B) : (G < B ? G : B);
1175 H = 60*(G-B)/(MAX-MIN)+0;
1177 H = 60*(G-B)/(MAX-MIN)+360;
1179 H = 60*(B-R)/(MAX-MIN)+120;
1181 H = 60*(R-G)/(MAX-MIN)+240;
1183 S = MAX == 0 ? 0 : 1 - MIN/MAX;
1191 void transformHSV_RGB(float *components) //H,S,B -> R,G,B
1193 float R = 0.0, G = 0.0, B = 0.0;
1194 float H = fmodf(components[0],359), //map to [0,360)
1198 int Hi = (int)floorf(H/60.) % 6;
1206 case 0: R=V;G=t;B=p; break;
1207 case 1: R=q;G=V;B=p; break;
1208 case 2: R=p;G=V;B=t; break;
1209 case 3: R=p;G=q;B=V; break;
1210 case 4: R=t;G=p;B=V; break;
1211 case 5: R=V;G=p;B=q; break;
1219 void resolveHSV(float *color1, float *color2) //H value may be undefined (i.e. graycale color)
1220 { // we want to fill it with a sensible value
1221 if(isnan(color1[0]) && isnan(color2[0]))
1222 color1[0] = color2[0] = 0;
1223 else if(isnan(color1[0]))
1224 color1[0] = color2[0];
1225 else if(isnan(color2[0]))
1226 color2[0] = color1[0];