Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Grafik / Sound / Multimedia (https://www.delphipraxis.net/21-library-grafik-sound-multimedia/)
-   -   Delphi RGB <--> HLS <--> HSI (https://www.delphipraxis.net/49002-rgb-hls-hsi.html)

cs_um 4. Jul 2005 08:58


RGB <--> HLS <--> HSI
 
Liste der Anhänge anzeigen (Anzahl: 1)
An dieser Stelle ein großes Danke, an alle, die mitgeholfen haben. Vielleicht hilft ja der Code auch irgendwem. :)

Da ich keine Umrechnung von HLS in HSI und umgekehrt gefunden habe, musste ich improvisieren:
Möglich waren folgende Umrechnungen:
  • RGB --> HLS
  • RGB --> HSI
  • HLS --> RGB
  • HSI --> RGB

Daraus ergibt sich die Umrechnung von HLS in HSI:
  • HLS --> RGB ; RGB --> HSI
  • HSI --> RGB ; RGB --> HLS


Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ScrollBar1: TScrollBar;
    ScrollBar2: TScrollBar;
    ScrollBar3: TScrollBar;
    ScrollBar4: TScrollBar;
    ScrollBar5: TScrollBar;
    ScrollBar6: TScrollBar;
    ScrollBar7: TScrollBar;
    ScrollBar8: TScrollBar;
    ScrollBar9: TScrollBar;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    RGB_R: TLabel;
    RGB_G: TLabel;
    RGB_B: TLabel;
    HLS_H: TLabel;
    HLS_L: TLabel;
    HLS_S: TLabel;
    HSV_H: TLabel;
    HSV_S: TLabel;
    HSV_V: TLabel;
    Farbfeld: TShape;
    procedure RGB(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
    procedure HLS(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
    procedure HSV(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
    procedure ScrollBarChange(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  Label1: TLabel;

implementation

{$R *.DFM}

//---------------------------------------
//-----------------RGB-------------------
//---------------------------------------

procedure TForm1.RGB(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);

  var RGB_R, RGB_G, RGB_B : Real;
      HLS_H, HLS_L, HLS_S : Real;
      HSV_H, HSV_S, HSV_V : Real;

      max_Farbwert : real;
      min_Farbwert : real;
      Differenz : real;
      Summe : real;

      RGB_R_temp1, RGB_G_temp1, RGB_B_temp1 : Real;
      RGB_R_temp2, RGB_G_temp2, RGB_B_temp2 : Real;

      HLS_H_temp, HLS_L_temp, HLS_S_temp : Real;
      HSV_H_temp, HSV_S_temp, HSV_V_temp : Real;

begin

  RGB_R_temp2:=0;
  RGB_G_temp2:=0;
  RGB_B_temp2:=0;

  RGB_R := Scrollbar1.Position;
  RGB_G := Scrollbar2.Position;
  RGB_B := Scrollbar3.Position;

  max_Farbwert := max(RGB_R, max(RGB_G,RGB_B));
  min_Farbwert := min(RGB_R, min(RGB_G,RGB_B));
  Differenz := max_Farbwert - min_Farbwert;
  Summe := max_Farbwert + min_Farbwert;


//Berechnung RGB-->HLS

  RGB_R_temp1:=RGB_R/255;
  RGB_G_temp1:=RGB_G/255;
  RGB_B_temp1:=RGB_B/255;

  HLS_L:=((max_Farbwert+min_Farbwert)/2);

  if min_Farbwert=max_Farbwert then
  begin
    HLS_H:=0;
    HLS_S:=0;
  end;

  if (((RGB_R = 0) and (RGB_G = 0) and (RGB_B = 0)) or
     ((RGB_R = 255) and (RGB_G = 255) and (RGB_B = 255)))
  then
    begin
      if ((RGB_R = 0) and (RGB_G = 0) and (RGB_B = 0))
      then
        begin
          HLS_L := 0;
          HLS_S := 0;
          HLS_H := 0;

        end;

      if ((RGB_R = 255) and (RGB_G = 255) and (RGB_B = 255))
      then
        begin
          HLS_L := 255;
          HLS_S := 0;
          HLS_H := 0;
        end;
    end

  else
    begin

      if ((min_Farbwert <> max_Farbwert))
      then
        begin
          if (HLS_L < 50) then HLS_S:=abs((max_Farbwert-min_Farbwert)/(max_Farbwert + min_Farbwert));
          if (HLS_L >= 50) then HLS_S:=abs((max_Farbwert-min_Farbwert)/(2-max_Farbwert-min_Farbwert));

          RGB_R_temp2 := (RGB_R-min_Farbwert)/(max_Farbwert-min_Farbwert);
          RGB_G_temp2 := (RGB_G-min_Farbwert)/(max_Farbwert-min_Farbwert);
          RGB_B_temp2 := (RGB_B-min_Farbwert)/(max_Farbwert-min_Farbwert);

          if (RGB_R=max_Farbwert) then HLS_H:= (RGB_G_temp2-RGB_B_temp2);
          if (RGB_G=max_Farbwert) then HLS_H:=2+(RGB_B_temp2-RGB_R_temp2);
          if (RGB_B=max_Farbwert) then HLS_H:=4+(RGB_R_temp2-RGB_G_temp2);
        end;

       if not((RGB_R=RGB_G) and (RGB_R=RGB_B)) then HLS_H:=(HLS_H+2)*60;

        if (HLS_H<0) then HLS_H:=(HLS_H+360);
        if (HLS_H>360) then HLS_H:=(HLS_H-360);

      end;

      HLS_L:=HLS_L*100/255;
      HLS_S:=HLS_S*100;

  scrollbar4.Position := round(HLS_H);
  scrollbar5.Position := round(HLS_L);
  scrollbar6.Position := round(HLS_S);

  Farbfeld.Brush.Color:=StrToInt('$'+((inttohex(scrollbar3.Position,2)+ //Blau-Anteil
                                    inttohex(scrollbar2.Position,2)+    //Grün-Anteil
                                    inttohex(scrollbar1.Position,2)))); // Rot-Anteil

//Berechnung RGB-->HSV

  RGB_R := Scrollbar1.Position;
  RGB_G := Scrollbar2.Position;
  RGB_B := Scrollbar3.Position;

  HSV_V := max_Farbwert/2.55;

  if (max_Farbwert = 0) then HSV_S := 0;
  if (max_Farbwert > 0) then HSV_S := (max_Farbwert - min_Farbwert) / 2.55;

  if (HSV_S = 0) then HSV_H:=0;
  if (((max_Farbwert-min_Farbwert) > 0) and (HSV_S > 0)) then

  begin
    if (max_Farbwert = RGB_R) then HSV_H := 60*((RGB_G-RGB_B)/Differenz);
    if (max_Farbwert = RGB_G) then HSV_H := 60*(2+(RGB_B-RGB_R)/Differenz);
    if (max_Farbwert = RGB_B) then HSV_H := 60*(4+(RGB_R-RGB_G)/Differenz);

    if (HSV_H < 0) then HSV_H := HSV_H + 360.0;
  end;

  scrollbar7.Position := round(HSV_H);
  scrollbar8.Position := round(HSV_S);
  scrollbar9.Position := round(HSV_V);

  Farbfeld.Brush.Color:=StrToInt('$'+((inttohex(scrollbar3.Position,2)+ //Blau-Anteil
                                    inttohex(scrollbar2.Position,2)+    //Grün-Anteil
                                    inttohex(scrollbar1.Position,2)))); // Rot-Anteil

end;



//---------------------------------------
//-----------------HLS-------------------
//---------------------------------------


procedure TForm1.HLS(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);

  var RGB_R, RGB_G, RGB_B : Real;
      HLS_H, HLS_L, HLS_S : Real;
      HSV_H, HSV_S, HSV_V : Real;

      max_Farbwert : real;
      min_Farbwert : real;
      Differenz : real;
      Summe : real;

      RGB_R_temp1, RGB_G_temp1, RGB_B_temp1 : Real;
      RGB_R_temp2, RGB_G_temp2, RGB_B_temp2 : Real;

      HLS_H_temp, HLS_L_temp, HLS_S_temp : Real;
      HSV_H_temp, HSV_S_temp, HSV_V_temp : Real;
      RGB_R_temp, RGB_G_temp, RGB_B_temp : Real;

      ganz: integer; rest: real;
      d, w, x, y, z:real;
      L1, L2: real;

begin

  HLS_H := Scrollbar4.Position;
  HLS_L := Scrollbar5.Position;
  HLS_S := Scrollbar6.Position;

//Berechnung HLS-->RGB

  HLS_H_temp:=HLS_H/360;
  HLS_L_temp:=HLS_L/100;
  HLS_S_temp:=HLS_S/100;

  if HLS_S_temp=0 then
    begin RGB_R:=HLS_L;
          RGB_G:=HLS_L;
          RGB_B:=HLS_L;
    end;

  if HLS_L_temp < 0.5 then L2 := HLS_L_temp * (1 + HLS_S_temp);
  if HLS_L_temp >= 0.5 then L2 := HLS_L_temp + HLS_S_temp - (HLS_L_temp * HLS_S_temp);
  L1:=2*HLS_L_temp - L2;

  RGB_R_temp := HLS_H_temp + (1/3);
  if RGB_R_temp < 0 then RGB_R_temp:=RGB_R_temp+1;
  if RGB_R_temp > 1 then RGB_R_temp:=RGB_R_temp-1;
  if (6*RGB_R_temp)<1 then RGB_R_temp:=L1+(L2-L1)*6*RGB_R_temp
  else if (2*RGB_R_temp) < 1 then RGB_R_temp:=L2
  else if (3*RGB_R_temp) < 2 then
    RGB_R_temp:=L1+(L2-L1)*((2/3)-RGB_R_temp)*6
  else RGB_R_temp:=L1;

  RGB_G_temp:=HLS_H_temp;
  if RGB_G_temp<0 then RGB_G_temp:=RGB_G_temp+1;
  if RGB_G_temp>1 then RGB_G_temp:=RGB_G_temp-1;
  if (6*RGB_G_temp)<1 then RGB_G_temp:=L1+(L2-L1)*6*RGB_G_temp
  else if (2*RGB_G_temp)<1 then RGB_G_temp:=L2
  else if (3*RGB_G_temp)<2 then
    RGB_G_temp:=L1+(L2-L1)*((2/3)-RGB_G_temp)*6
  else RGB_G_temp:=L1;

  RGB_B_temp:=HLS_H_temp-(1/3);
  if RGB_B_temp<0 then RGB_B_temp:=RGB_B_temp+1;
  if RGB_B_temp>1 then RGB_B_temp:=RGB_B_temp-1;
  if (6*RGB_B_temp)<1 then RGB_B_temp:=L1+(L2-L1)*6*RGB_B_temp
  else if (2*RGB_B_temp)<1 then RGB_B_temp:=L2
  else if (3*RGB_B_temp)<2 then
    RGB_B_temp:=L1+(L2-L1)*((2/3)-RGB_B_temp)*6
  else RGB_B_temp:=L1;

  RGB_R:=RGB_G_temp*255;
  RGB_G:=RGB_B_temp*255;
  RGB_B:=RGB_R_temp*255;


  scrollbar1.Position := round(RGB_R);
  scrollbar2.Position := round(RGB_G);
  scrollbar3.Position := round(RGB_B);

  Farbfeld.Brush.Color:=StrToInt('$'+((inttohex(scrollbar3.Position,2)+ //Blau-Anteil
                                    inttohex(scrollbar2.Position,2)+    //Grün-Anteil
                                    inttohex(scrollbar1.Position,2)))); // Rot-Anteil

//************************************************
//************************************************

  RGB_R := Scrollbar1.Position;
  RGB_G := Scrollbar2.Position;
  RGB_B := Scrollbar3.Position;

  max_Farbwert := max(RGB_R, max(RGB_G,RGB_B));
  min_Farbwert := min(RGB_R, min(RGB_G,RGB_B));
  Differenz := max_Farbwert - min_Farbwert;
  Summe := max_Farbwert + min_Farbwert;

//Berechnung RGB-->HSV

  HSV_V := max_Farbwert/2.55;

  if (max_Farbwert = 0) then HSV_S := 0;
  if (max_Farbwert > 0) then HSV_S := (max_Farbwert - min_Farbwert) / 2.55;

  if (HSV_S = 0) then HSV_H:=0;
  if (((max_Farbwert-min_Farbwert) > 0) and (HSV_S > 0)) then

  begin
    if (max_Farbwert = RGB_R) then HSV_H := 60*((RGB_G-RGB_B)/Differenz);
    if (max_Farbwert = RGB_G) then HSV_H := 60*(2+(RGB_B-RGB_R)/Differenz);
    if (max_Farbwert = RGB_B) then HSV_H := 60*(4+(RGB_R-RGB_G)/Differenz);

    if (HSV_H < 0) then HSV_H := HSV_H + 360.0;
  end;

  scrollbar7.Position := round(HSV_H);
  scrollbar8.Position := round(HSV_S);
  scrollbar9.Position := round(HSV_V);



  Farbfeld.Brush.Color:=StrToInt('$'+((inttohex(scrollbar3.Position,2)+    //Blau-Anteil
                                    inttohex(scrollbar2.Position,2)+    //Grün-Anteil
                                    inttohex(scrollbar1.Position,2)))); // Rot-Anteil
end;



//---------------------------------------
//-----------------HSV-------------------
//---------------------------------------

procedure TForm1.HSV(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);

  var RGB_R, RGB_G, RGB_B : Real;
      HLS_H, HLS_L, HLS_S : Real;
      HSV_H, HSV_S, HSV_V : Real;

      max_Farbwert : real;
      min_Farbwert : real;
      Differenz : real;
      Summe : real;

      RGB_R_temp1, RGB_G_temp1, RGB_B_temp1 : Real;
      RGB_R_temp2, RGB_G_temp2, RGB_B_temp2 : Real;

      HLS_H_temp, HLS_L_temp, HLS_S_temp : Real;
      HSV_H_temp, HSV_S_temp, HSV_V_temp : Real;

      a,b,c:real;
      ganz: integer; rest: real;
begin

  HSV_H := Scrollbar7.Position;
  HSV_S := Scrollbar8.Position;
  HSV_V := Scrollbar9.Position;

//Berechnung HSV-->RGB

  if (HSV_S = 0) then
  begin RGB_R := HSV_V*2.55;
        RGB_G := HSV_V*2.55;
        RGB_B := HSV_V*2.55;
  end;

  if (HSV_S <> 0) then
  begin

    if (HSV_H = 360) then HSV_H:=0;

    HSV_H:=HSV_H/60;

    Ganz:=Trunc(HSV_H);  // ganzzahliger Teil von HSV_H
    Rest:=Frac(HSV_H);   // Nachkommastellen von HSV_H


    HSV_S:=HSV_S/100;
    HSV_V:=HSV_V/100;

    a:=HSV_V*(1-HSV_S);
    b:=HSV_V*(1-(HSV_S*Rest));
    c:=HSV_V*(1-(HSV_S*(1-Rest)));

    a:=a*2.55;
    b:=b*2.55;
    c:=c*2.55;
    HSV_V:=HSV_V*2.55;

    case Ganz of
         0: begin RGB_R:=HSV_V*100;
            RGB_G:=c*100;
            RGB_B:=a*100; end;

         1: begin RGB_R:=b*100;
            RGB_G:=HSV_V*100;
            RGB_B:=a*100; end;

         2: begin RGB_R:=a*100;
            RGB_G:=HSV_V*100;
            RGB_B:=c*100; end;

         3: begin RGB_R:=a*100;
            RGB_G:=b*100;
            RGB_B:=HSV_V*100; end;

         4: begin RGB_R:=c*100;
            RGB_G:=a*100;
            RGB_B:=HSV_V*100; end;

         5: begin RGB_R:=HSV_V*100;
            RGB_G:=a*100;
            RGB_B:=b*100;end;

  end;
end;

  scrollbar1.Position := round(RGB_R);
  scrollbar2.Position := round(RGB_G);
  scrollbar3.Position := round(RGB_B);

//************************************************
//************************************************



  RGB_R_temp2:=0;
  RGB_G_temp2:=0;
  RGB_B_temp2:=0;

  RGB_R := Scrollbar1.Position;
  RGB_G := Scrollbar2.Position;
  RGB_B := Scrollbar3.Position;

  max_Farbwert := max(RGB_R, max(RGB_G,RGB_B));
  min_Farbwert := min(RGB_R, min(RGB_G,RGB_B));
  Differenz := max_Farbwert - min_Farbwert;
  Summe := max_Farbwert + min_Farbwert;


//Berechnung RGB-->HLS

  RGB_R_temp1:=RGB_R/255;
  RGB_G_temp1:=RGB_G/255;
  RGB_B_temp1:=RGB_B/255;

  HLS_L:=((max_Farbwert+min_Farbwert)/2);

  if min_Farbwert=max_Farbwert then
  begin
    HLS_H:=0;
    HLS_S:=0;
  end;

  if (((RGB_R = 0) and (RGB_G = 0) and (RGB_B = 0)) or
     ((RGB_R = 255) and (RGB_G = 255) and (RGB_B = 255)))
  then
    begin
      if ((RGB_R = 0) and (RGB_G = 0) and (RGB_B = 0))
      then
        begin
          HLS_L := 0;
          HLS_S := 0;
          HLS_H := 0;

        end;

      if ((RGB_R = 255) and (RGB_G = 255) and (RGB_B = 255))
      then
        begin
          HLS_L := 255;
          HLS_S := 0;
          HLS_H := 0;
        end;
    end

  else
    begin

      if ((min_Farbwert <> max_Farbwert))
      then
        begin
          if (HLS_L < 50) then HLS_S:=abs((max_Farbwert-min_Farbwert)/(max_Farbwert + min_Farbwert));
          if (HLS_L >= 50) then HLS_S:=abs((max_Farbwert-min_Farbwert)/(2-max_Farbwert-min_Farbwert));

          RGB_R_temp2 := (RGB_R-min_Farbwert)/(max_Farbwert-min_Farbwert);
          RGB_G_temp2 := (RGB_G-min_Farbwert)/(max_Farbwert-min_Farbwert);
          RGB_B_temp2 := (RGB_B-min_Farbwert)/(max_Farbwert-min_Farbwert);

          if (RGB_R=max_Farbwert) then HLS_H:= (RGB_G_temp2-RGB_B_temp2);
          if (RGB_G=max_Farbwert) then HLS_H:=2+(RGB_B_temp2-RGB_R_temp2);
          if (RGB_B=max_Farbwert) then HLS_H:=4+(RGB_R_temp2-RGB_G_temp2);
        end;

       if not((RGB_R=RGB_G) and (RGB_R=RGB_B)) then HLS_H:=(HLS_H+2)*60;

        if (HLS_H<0) then HLS_H:=(HLS_H+360);
        if (HLS_H>360) then HLS_H:=(HLS_H-360);

      end;

      HLS_L:=HLS_L*100/255;
      HLS_S:=HLS_S*100;

  scrollbar4.Position := round(HLS_H);
  scrollbar5.Position := round(HLS_L);
  scrollbar6.Position := round(HLS_S);




  Farbfeld.Brush.Color:=StrToInt('$'+((inttohex(scrollbar3.Position,2)+ //Blau-Anteil
                                    inttohex(scrollbar2.Position,2)+    //Grün-Anteil
                                    inttohex(scrollbar1.Position,2)))); // Rot-Anteil


end;

//---------------------------------------
//----------Labels beschriften-----------
//---------------------------------------

procedure TForm1.ScrollBarChange(Sender: TObject);
begin
  label1.caption:=inttostr(scrollbar1.Position ); //Position ausgeben (Zahl)
  label2.caption:=inttostr(scrollbar2.Position ); //Position ausgeben (Zahl)
  label3.caption:=inttostr(scrollbar3.Position ); //Position ausgeben (Zahl)

  label4.caption:=inttostr(scrollbar4.Position ); //Position ausgeben (Zahl)
  label5.caption:=inttostr(scrollbar5.Position ); //Position ausgeben (Zahl)
  label6.caption:=inttostr(scrollbar6.Position ); //Position ausgeben (Zahl)

  label7.caption:=inttostr(scrollbar7.Position ); //Position ausgeben (Zahl)
  label8.caption:=inttostr(scrollbar8.Position ); //Position ausgeben (Zahl)
  label9.caption:=inttostr(scrollbar9.Position ); //Position ausgeben (Zahl)
end;

END.


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:27 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz