Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Luminosidad de color sin HSV (https://www.clubdelphi.com/foros/showthread.php?t=80882)

coso 14-08-2008 17:20:20

Luminosidad de color sin HSV
 
Hola, adjunto un codigo para cambiar la luminosidad de un color sin tener que pasar de RGB a HSV.

Código Delphi [-]
unit llumin;

interface

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

type
  TForm1 = class(TForm)
    TrackBar1: TTrackBar;
    Shape1: TShape;
    ColorDialog1: TColorDialog;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    function Red(c : TColor) : integer;
    function Green(c : TColor) : integer;
    function Blue(c : TColor) : integer;
    function Lluminositat(c : TColor; tpc : double) : TColor;
    procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.Lluminositat(c : TColor; tpc : double): TColor;
var
        r,g,b : integer;
        dr,dg,db : integer;
        rest  : integer;
        act : integer;
        rest_i : integer;
begin
       r := Red(c);
       g := Green(c);
       b := Blue(c);

       rest := Round(3*tpc - (r+g+b));
       rest_i := rest div 3;
       act := r+g+b;

       if rest > 0 then
       while rest > 0 do
       begin
                if r < $FF then begin inc(r); dec(rest); end;
                if g < $FF then begin inc(g); dec(rest); end;
                if b < $FF then begin inc(b); dec(rest); end;
       end
       else
       while rest < 0 do
       begin
                if r > 0 then begin dec(r); inc(rest); end;
                if g > 0 then begin dec(g); inc(rest); end;
                if b > 0 then begin dec(b); inc(rest); end;
       end;

        Shape2.Width := r;
        Shape3.Width := g;
        Shape4.Width := b;
        Shape5.Width := Round(act div 3);
        Shape6.Left  := Shape5.Width + Shape5.Left;
        Shape6.Width := Round(rest_i);
        Shape7.Width := Round(tpc);

       Lluminositat := TColor(r*$01 + g*$0100 + b*$010000);
end;

function TForm1.Red(c : TColor) : integer;
begin
        Red := c and $FF;
end;

function TForm1.Green(c : TColor) : integer;
begin
        Green := (c and $FF00) div $100;
end;

function TForm1.Blue(c : TColor) : integer;
begin
        Blue := (c and $FF0000) div $10000;
end;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
        c : TColor;
begin
        if ColorDialog1.Execute then
        begin
                c := ColorDialog1.Color;
                Shape1.Brush.Color := C;
                TrackBar1.Position := (red(c) + green(c) + blue(c)) div 3;
                Color := Lluminositat(c,TrackBar1.Position);
                Update;
        end;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
        Color := Lluminositat(Shape1.Brush.Color,TrackBar1.Position);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
        doublebuffered := true;
        TrackBar1.TickStyle := tsNone;
        TrackBar1.Max := 255;
end;

end.

la funcion es la de luminosidad, aunque esta integrada en el ejemplo, que usa varios Shape, un trackbar y un colordialog


La franja horaria es GMT +2. Ahora son las 20:06:02.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi