CGContextSetRGBFillColor 未设置正确的颜色

发布于 2024-09-25 05:47:52 字数 1701 浏览 3 评论 0原文

我的问题:正方形填充的颜色从黑色变为全蓝色(255),没有过渡颜色(深蓝色、深蓝色、蓝色...)。看来 CGContextSetRGBStroke 是附加的(WTF Oo)。就像如果蓝色是 14 而下一次更新我把 140 放在蓝色将是 154 而不是我设置的 140 。

有人遇到这个问题吗?

在.h中

    CGFloat angle;
    int width;
    int height;
    NSTimer* timer; 
    CGPoint touch;

在.m中

 - (id)initWithFrame:(CGRect)frame 
{
  if ((self = [super initWithFrame:frame])) 
    {
 angle=0;
 width = frame.size.width;
 height= frame.size.height;
 //self.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:1 alpha:1];
 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update:) userInfo:nil repeats:YES];
    }
    return self;
}

 -(void)update:(NSTimer*)theTimer
  {
     [self setNeedsDisplay];
  }

 NS_INLINE CGFloat radians(CGFloat ang)
 {
  return ang*(M_PI/180.0f);
 }

 - (void)drawRect:(CGRect)rect 
 {
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //CGContextSetRGBFillColor(ctx, -1,-1,-1,-1);

    CGContextSaveGState(ctx);
    CGRect ourRect = CGRectMake(40+angle, 40+angle, 240, 120);

    CGFloat colour=(touch.y/768)*255;
    NSQLog(@"draw %f",colour);
    CGContextSetRGBFillColor(ctx, 0.0,0.0,colour,1);
    CGContextClearRect(ctx, rect);

    CGContextFillRect(ctx, ourRect);

    CGContextSetRGBStrokeColor(ctx, 0.0, 1.0, 0.0, 1.0);
    CGContextStrokeRectWithWidth(ctx, ourRect, 10);
    CGContextRestoreGState(ctx);

    angle+=1;  
 }

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
       touch= [[touches anyObject] locationInView:self];
 }

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
       touch= [[touches anyObject] locationInView:self]; 
  }

My problem : the color of the fill in square go from black to full blue (255) with no transition color (darkest blue, dark blue, blue...). It seems that the CGContextSetRGBStroke is additive (WTF Oo) . like if the blue was 14 and the next update I put 140 the blue will be 154 and not 140 has I set.

Is someone get that problem ?

in the .h

    CGFloat angle;
    int width;
    int height;
    NSTimer* timer; 
    CGPoint touch;

in the .m

 - (id)initWithFrame:(CGRect)frame 
{
  if ((self = [super initWithFrame:frame])) 
    {
 angle=0;
 width = frame.size.width;
 height= frame.size.height;
 //self.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:1 alpha:1];
 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update:) userInfo:nil repeats:YES];
    }
    return self;
}

 -(void)update:(NSTimer*)theTimer
  {
     [self setNeedsDisplay];
  }

 NS_INLINE CGFloat radians(CGFloat ang)
 {
  return ang*(M_PI/180.0f);
 }

 - (void)drawRect:(CGRect)rect 
 {
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //CGContextSetRGBFillColor(ctx, -1,-1,-1,-1);

    CGContextSaveGState(ctx);
    CGRect ourRect = CGRectMake(40+angle, 40+angle, 240, 120);

    CGFloat colour=(touch.y/768)*255;
    NSQLog(@"draw %f",colour);
    CGContextSetRGBFillColor(ctx, 0.0,0.0,colour,1);
    CGContextClearRect(ctx, rect);

    CGContextFillRect(ctx, ourRect);

    CGContextSetRGBStrokeColor(ctx, 0.0, 1.0, 0.0, 1.0);
    CGContextStrokeRectWithWidth(ctx, ourRect, 10);
    CGContextRestoreGState(ctx);

    angle+=1;  
 }

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
       touch= [[touches anyObject] locationInView:self];
 }

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
       touch= [[touches anyObject] locationInView:self]; 
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-10-02 05:47:52

只是一个快速回答,因为我看到以下几行:

CGFloat colour=(touch.y/768)*255;
CGContextSetRGBFillColor(ctx, 0.0,0.0,colour,1);

您应该将颜色部分指定为范围从 0.0f 到 1.0f 的 CGFloat。难道你的蓝色部分处于“0到255模式”?

编辑

根据您的代码判断,我认为您可以在颜色计算中省略乘以 255。当 y 为 0 时,蓝色分量为 0.0f;当 y 为 768 时,蓝色分量为 1.0f。

Just a quick answer as I'm seeing the following lines:

CGFloat colour=(touch.y/768)*255;
CGContextSetRGBFillColor(ctx, 0.0,0.0,colour,1);

You should specify the color parts as a CGFloat ranging from 0.0f to 1.0f. Could it be your blue color part is in the "0 to 255 mode"?

Edit

Judging by your code, I think you can leave out the multiply by 255 in the color calculation. When y is 0 you'll have 0.0f as blue color component and when y is 768 it'll be 1.0f.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文