Delphi实现RGB色环的代码绘制

       突发奇想要做一个和颜色管理有关系的小工具,其中有一个小功能就是拾色器,本打算仿照PHOTOSHOP的拾色器,可是Adobe用的是HSB方式,我不会,只好考虑改用Paint.net的色环拾色器。

        色环对于颜色选取并不合适,越靠近圆心的地方,取得的颜色连续性越差,只能通过手工微调来调整到满意的颜色,而且色环中没有黑色,如果绘图算法不好且圆比较小,也可能没有纯白。所以这个色环只能适用于大致的颜色选取。

RGB色环

        网上对于色环的纯代码绘制算法不是很多,我只找到了一个用易语言实现的,我把它翻译成了Delphi。下面是代码。

function TColorFrm.CreateColorCircle(const size: integer): TBitmap;
var
  i,j,x,y: Integer;
  radius: integer;
  perimeter,arc,degree,step: double;
  R,G,B: byte;
  color: TColor;
begin
  radius := round(size / 2);
  RESULT := TBitmap.Create;
  R:=255;
  G:=0;
  B:=0;
  with RESULT do
  begin
    width := size;
    height:= size;
    pixelFormat := pf24bit;
    Canvas.Brush.Color := RGB(R,G,B);
    x := size + 1;
    y := round(radius) + 1;
    Canvas.FillRect(Rect(size,round(radius),x,y));
    for j := 0 to size do
      begin
      perimeter := (size - j) * PI + 1;
      arc := perimeter / 6;
      step := ( 255 * 6 ) / perimeter ; //颜色渐变步长
      for i := 0 to round(perimeter) - 1 do
        begin
          degree := 360 / perimeter * i;
          x := round(cos(degree * PI / 180) * (size - j + 1) / 2) + radius;//数学公式,最后加上的是圆心点
          y := round(sin(degree * PI / 180) * (size - j + 1) / 2) + radius;

          if (degree > 0) and (degree <= 60) then
          begin
            R := 255;
            G := 0;
            B := round(step * i);
          end;
          if (degree > 60) and (degree <= 120) then
          begin
            if perimeter / 3 / 120 * (degree - 60) > 1.0 then
              R := 255 - round(step * (i - arc))
            else
              R := 255 - round(step * ABS(i - arc));
            G := 0;
            B := 255;
          end;
          if (degree > 120) and (degree <= 180) then
          begin
            R := 0;
            if perimeter / 3 / 120 * (degree - 120) > 1.0 then
              G := round(step * (i - 2 * arc))
            else
              G := round(step * ABS(i - 2 * arc));
            B := 255;
          end;
          if (degree > 180) and (degree <= 240) then
          begin
            R := 0;
            G := 255;
            if perimeter / 3 / 120 * (degree - 120) > 1.0 then
              B := 255 - round(step * (i - perimeter / 2))
            else
              B := 255 - round(step * ABS(i - perimeter / 2));
          end;
          if (degree > 240) and (degree <= 300) then
          begin
            if perimeter / 3 / 120 * (degree - 240) > 1.0 then
              R := round(step * (i - 4 * arc))
            else
              R := round(step * ABS(i - 4 * arc)) ;
            G := 255;
            B := 0;
          end;
          if (degree > 300) and (degree <= 360) then
          begin
            R := 255;
            if perimeter / 3 / 120 * (degree - 300) > 1.0 then
              G := 255 - round(step * (i - 5 * arc))
            else
              G := 255 - round(step * ABS(i - 5 * arc));
            B := 0;
          end;
          color := RGB( ROUND(R + (255 - R)/size * j),ROUND(G + (255 - G) / size * j),ROUND(B + (255 - B) / size * j));
          Canvas.Brush.Color := color;
          //为了绘制出来的圆好看,分成四个部分进行绘制
          if (degree >= 0) and (degree <= 45) then
            Canvas.FillRect(Rect(x,y,x-2,y-1));
          if (degree > 45) and (degree <= 135) then
            Canvas.FillRect(Rect(x,y,x-1,y-2));
          if (degree > 135) and (degree <= 225) then
            Canvas.FillRect(Rect(x,y,x+2,y+1));
          if (degree > 215) and (degree <= 315) then
            Canvas.FillRect(Rect(x,y,x+1,y+2));
          if (degree > 315) and (degree <= 360) then
            Canvas.FillRect(Rect(x,y,x-2,y-1));
        end;
      end;
  end;
end;
       使用Image控件作为画板,为了好看,可以把透明属性勾选,这样就不会有背景色了,size为Image控件的宽度(因为是画圆,所以请务必使Image控件长宽相等)。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值