Skip to content

Instantly share code, notes, and snippets.

@boucher
Created May 14, 2009 06:19
Show Gist options
  • Select an option

  • Save boucher/111521 to your computer and use it in GitHub Desktop.

Select an option

Save boucher/111521 to your computer and use it in GitHub Desktop.
var context = [[CPGraphicsContext currentContext] graphicsPort],
bottomBorderColor = [CPColor colorWithWhite:0.0 alpha:0.6],
topBorderColor = [CPColor colorWithWhite:1.0 alpha:0.6],
tintColor = _tintColor || [self backgroundColor],
bounds = [self bounds];
CGContextSetFillColor(context, tintColor);
CGContextFillRect(context, bounds);
CGContextBeginPath(context);
CGContextAddRect(context, bounds);
CGContextClosePath(context);
var hsb = [tintColor hsbComponents],
hue = hsb[0],
saturation = hsb[1],
brightness = hsb[2],
color1, color2, color3, color4;
if (brightness < 20)
{
color1 = [CPColor colorWithHue:hue saturation:MAX(saturation * 0.4, 0) brightness:MIN(brightness + 30, 100)];
color2 = [CPColor colorWithHue:hue saturation:MAX(saturation * 0.95, 0) brightness:MIN(brightness + 2, 100)];
color3 = tintColor;
color4 = [CPColor colorWithHue:hue saturation:MIN(saturation * 1.2, 100) brightness:MAX(brightness * 0.98, 0)];
}
else
{
color1 = [CPColor colorWithHue:hue saturation:MAX(saturation * 0.4, 0) brightness:MIN(brightness * 1.2, 100)];
color2 = [CPColor colorWithHue:hue saturation:MAX(saturation * 0.95, 0) brightness:MIN(brightness * 1.02, 100)];
color3 = tintColor;
color4 = [CPColor colorWithHue:hue saturation:MIN(saturation * 1.2, 100) brightness:MAX(brightness * 0.98, 0)];
}
var gradient = [CPColor newGradientWithColors:[color1, color2, color3, color4] locations:[0.0, 0.5, 0.501, 1.0] count:4];
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, CGRectGetHeight(bounds)), 0);
CGContextSetFillColor(context, topBorderColor);
CGContextFillRect(context, CGRectMake(0, 0, CGRectGetWidth(bounds), 1.0));
CGContextSetFillColor(context, bottomBorderColor);
CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(bounds) - 1.0, CGRectGetWidth(bounds), 1.0));
+ (CGGradientRef)newGradientWithColors:(CPArray)colors locations:(CPArray)locations count:(int)count
{
var components = [],
computedLocations = [],
offset = 1.0/count,
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
for (var i=0; i < count; ++i)
{
var color = colors[i],
rgba = [color components];
components[i*4] = rgba[0];
components[i*4+1] = rgba[1];
components[i*4+2] = rgba[2];
components[i*4+3] = rgba[3];
computedLocations[count] = offset * i;
if (i>0 && i<count -1)
computedLocations[count] += 0.01;
}
var gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations ? locations : computedLocations, count);
return gradient;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment