How to paint 2 gradients together on a Canvas?

前端 未结 4 878
情深已故
情深已故 2021-02-08 11:16

Please look at this sample gradient image I did with a Paint program:

\"enter

It c

4条回答
  •  無奈伤痛
    2021-02-08 11:51

    I coded it using a normal TCanvas.

    The code draws a gradient on that canvas by steadily increasing the colors. You can adjust that for example by adding weights to either the start or the end color (e.g. to increase the white part).

    procedure drawGradient(drawCanvas: TCanvas; canvasHeight, canvasWidth, canvasStartPos: Integer; startColor, endColor: TColor);
    type
      RGBColor = (Blue, Green, Red);
    var
      diff, startColorArray, endColorArray: array[RGBColor] of Integer;
      delta, currentColorFloat: array[RGBColor] of Double;
      gradientSize: Integer;
      currentColor: TColor;
      rgbC: RGBColor;
      i: Integer;
    begin
      gradientSize := canvasHeight div 2;
    
      // Pre-calculate some required values for every RGB color
      for rgbC := Low(RGBColor) to High(RGBColor) do
        begin
        // Split the start end end colors into the RGB values
        // The right shift at the end shifts 16, 8 and 0 bits in the three loops
        // (I know that's a little hard to read)
    
        startColorArray[rgbC] := $FF and (startColor shr ((2 - Ord(rgbC)) * 8));
        endColorArray[rgbC] := $FF and (endColor shr ((2 - Ord(rgbC)) * 8));
    
        // Calculate the difference between the start and end color. This might be
        // a negative value, hence the declaration as Integer instead of Byte
        diff[rgbC] := startColorArray[rgbC] - endColorArray[rgbC];
    
        // And calculate a float value for each color. This is the increment on
        // every drawn line.
        delta[rgbC] := diff[rgbC] / gradientSize;
        end;
    
      // Initialize the drawn color with the start value
      currentColorFloat[Blue] := startColorArray[Blue];
      currentColorFloat[Green] := startColorArray[Green];
      currentColorFloat[Red] := startColorArray[Red];
    
      // Now draw the gradient line by line
      for i := 0 to gradientSize - 1 do
        begin
        // The target color as TColor
        currentColor := 0;
    
        for rgbC := Low(RGBColor) to High(RGBColor) do
          begin
          // Substract the decrement delta from the current color
          currentColorFloat[rgbC] := currentColorFloat[rgbC] - delta[rgbC];
    
          // Round the float value and left shift it to the correct position (16, 8 and 0 bits).
          // Then bitwise or it with the current color.
          currentColor := currentColor or (Round(currentColorFloat[rgbC]) shl ((2 - Ord(rgbC)) * 8));
          end;
    
        // Now draw a 1 pixel thin line from left to right
        drawCanvas.Pen.Color := currentColor;
        drawCanvas.MoveTo(0, i + canvasStartPos);
        drawCanvas.LineTo(canvasWidth, i + canvasStartPos);
        end;
    end;
    

    Call it like this:

    procedure TForm18.Button1Click(Sender: TObject);
    const
      white1: TColor = clWhite;
      white2: TColor = $00CFCFCF;
      color1: TColor = $000080FF;
      color2: TColor = $00007AF4;
    begin
      // pb is a TPaintbox, but this works with any canvas
    
      drawGradient(pb.Canvas, pb.Height, pb.Width, 0, white1, color1);
      drawGradient(pb.Canvas, pb.Height, pb.Width, pb.Height div 2, color2, white2);
    end;
    

    The result looks like this:

    enter image description here

提交回复
热议问题