Ios CAGradientLayer在像素处获得颜色
我有一个带有许多颜色停止点的Ios CAGradientLayer在像素处获得颜色,ios,swift,gradient,cagradientlayer,Ios,Swift,Gradient,Cagradientlayer,我有一个带有许多颜色停止点的CAGradientLayer。假设我知道所选图层上的点。如何获得该位置的像素颜色 我试图在渐变上选择一个位置,但我不确定如何在其边界的特定点获得层的像素颜色 看看我的例子,它根据我的UISliders位置值0插值颜色。你看了吗?@luk2302我得到红色:1绿色:1,蓝色:1,阿尔法:1。我想知道这是否与它是一个渐变层有关。嗯,不知道,我希望这不会有什么关系,因为你告诉它将自己呈现到上下文中。。。我今天晚些时候会亲自检查。 -(UIColor *) getColor
CAGradientLayer
。假设我知道所选图层上的点。如何获得该位置的像素颜色
我试图在渐变上选择一个位置,但我不确定如何在其边界的特定点获得层的像素颜色 看看我的例子,它根据我的UISliders位置值0插值颜色。你看了吗?@luk2302我得到红色:1绿色:1,蓝色:1,阿尔法:1。我想知道这是否与它是一个渐变层有关。嗯,不知道,我希望这不会有什么关系,因为你告诉它将自己呈现到上下文中。。。我今天晚些时候会亲自检查。
-(UIColor *) getColor{
/* //My pre declared code(just for clarity)
@property (weak, nonatomic) IBOutlet UISlider *slider;
CAGradientLayer* gradientLayer;
float colorCutoff;
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor orangeColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor cyanColor].CGColor,(__bridge id)[UIColor blueColor].CGColor,(__bridge id)[UIColor purpleColor].CGColor];
//This below allows for easy one direction interpolation
gradientLayer.startPoint = CGPointMake(0.5,0.0);
gradientLayer.endPoint = CGPointMake(0.5,1.0f);
colorCutoff = 1/gradientLayer.colors.count;
*
*
*/
float interp = _slider.value;
// 1.0f * colors.count == index out of bounds
if(interp >= 1.0f){
interp = .99999f;
}
// think FLOOR(float * float)
int firstColor = (interp * (float)gradientLayer.colors.count);
int secondColor = firstColor +1;
// In case we are at the last color their is not one above it(index out of bounds)
if(secondColor >= gradientLayer.colors.count){
firstColor--;
secondColor--;
}
// In My case the closer you are to the colorCuttoff the more the second color should have a factor
// 0 is red .14 is orange .28 is yellow and soo on
float firstInterp = 1.0f - (fmodf(interp,colorCutoff) / colorCutoff);
float secondInterp = 1.0f - firstInterp;
// make use of the gradientLayer.colors array
const CGFloat *firstCGColor = CGColorGetComponents((__bridge CGColorRef)gradientLayer.colors[firstColor]);
const CGFloat *secondCGColor = CGColorGetComponents((__bridge CGColorRef)gradientLayer.colors[secondColor]);
float red = (firstCGColor[0] * firstInterp) + secondCGColor[0] * secondInterp;
float green = (firstCGColor[1] * firstInterp) + secondCGColor[1] * secondInterp;
float blue = (firstCGColor[2] * firstInterp) + secondCGColor[2] * secondInterp;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}