Search code examples
ioscocoa-touchuicolorcgcontext

Gradient color is too light in ios


I am trying to draw a gradient but my colors are off. To simplify let me just try to draw something of the same color as the background of my table view, i.e. [UIColor darkGrayColor]. My drawing code looks like this:

CGColorSpaceRef myColorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
redEndColor = 1./3.;
greenEndColor = 1./3.;
blueEndColor = 1./3.;
alphaEndColor = 1.;
redStartColor = redEndColor;
greenStartColor = greenEndColor;
blueStartColor = blueEndColor;
alphaStartColor = alphaEndColor;

CGFloat components[8] = {redStartColor, greenStartColor, blueStartColor, alphaStartColor, redEndColor, greenEndColor, blueEndColor, alphaEndColor };

CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components,locations, 2);
CGContextDrawLinearGradient(context, myGradient, myStartPoint, myEndPoint, 0);

But the drawn color is too light (see picture). What am I possibly doing wrong ?

enter image description here


Solution

  • For the record, the color space was wrong and I needed to replace the first line of the code snipper above by

    CGColorSpaceRef myColorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);