[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

How to make a gradient backgound on a form

I need to make a gradient form for one app I'm developping. I made one, but when i tried
to use my source with 26 colors it dind't work.
I need some piece of code that works on 8bit,16bit,24bit and 32bit(if possible it would be
of it worked also on 4bit-16 colors

IDEAS??

thanks

                 LR
0
lfrodrigues
Asked:
lfrodrigues
1 Solution
 
TheNeilCommented:
Try doing something along the lines of the following:

VAR
  r_step, g_step, b_step : REAL;
  r, g, b, n : INTEGER;
BEGIN
  r_step := (r1 - r2) / bmpBackdrop.Height;
  g_step := (g1 - g2) / bmpBackdrop.Height;
  b_step := (b1 - b2) / bmpBackdrop.Height;

  bmpBackdrop.PixelFormat := pf8bit;

  FOR n := 0 TO bmpBackdrop.Height
  DO
  BEGIN
    r := ROUND(r1 + n * r_step);
    g := ROUND(g1 + n * g_step);
    b := ROUND(b1 + n * b_step);
    bmpBackdrop.Canvas.Pen.Color := RGB(r, g, b);
    bmpBackdrop.Canvas.MoveTo(0, n);
    bmpBackdrop.Canvas.LineTo (bmpBackdrop.Width, n);
  END;
END;

You simply need to supply the two colours to gradiate between (r1, g1, b1 and r2, g2, b2) and it should work. You can also change the colour depth and it should still work fine with Windows picking the nearest colour that it can.

The Neil
0
 
intheCommented:
Hi
here's a way to try:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure PaintBackGround;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.PaintBackGround;
 var Clr1, Clr2: TColor; RGBFrom: array[0..2] of Byte; RGBDiff: array[0..2] of Integer;
  ColorBand: TRect; I: Integer; R, G, B: Byte; begin
 with Image1 do begin
  Height := Screen.Height;
  Width := Screen.Width;
  Left := 0;
  Top := 0;
  Clr1 := clNavy;
  Clr2 := clMaroon;
  RGBFrom[0] := GetRValue (ColorToRGB(Clr1));
  RGBFrom[1] := GetGValue (ColorToRGB(Clr1));
  RGBFrom[2] := GetBValue (ColorToRGB(Clr1));
  RGBDiff[0] := GetRValue (ColorToRGB(Clr2)) - RGBFrom[0];
  RGBDiff[1] := GetGValue (ColorToRGB(Clr2)) - RGBFrom[1];
  RGBDiff[2] := GetBValue (ColorToRGB(Clr2)) - RGBFrom[2];
  ColorBand.Left := 0;
  ColorBand.Right := Width;
  for I := 0 to $ff do begin
   ColorBand.Top := MulDiv (I, Height, $100);
   ColorBand.Bottom := MulDiv (I + 1, Height, $100);
   R := RGBFrom[0] + MulDiv (I, RGBDiff[0], $ff);
   G := RGBFrom[1] + MulDiv (I, RGBDiff[1], $ff);
   B := RGBFrom[2] + MulDiv (I, RGBDiff[2], $ff);
   Canvas.Brush.Color := RGB (R, G, B);
   Canvas.FillRect (ColorBand); end; end; end;

procedure TForm1.FormCreate(Sender: TObject);
begin
paintbackground;
end;

end.
0
 
drnadeemCommented:
How about this one ?

gradientfill(canvas,clientrect,clred,clblue,true);



Add Chart to uses clause.
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
intheCommented:
drnadeem ,
dont you think you should have posted comment only?

0
 
drnadeemCommented:
Sorry inthe, wasn't giong to steal your points. :)

0
 
lfrodriguesAuthor Commented:
inthe's anwser is by far the best. So inthe post a anwser so i can credit you
0
 
intheCommented:
Thankyou :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now