Autor Beitrag
Aya
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Mi 26.03.03 01:22 
Hi,

weiß jemand wie ich RGB nach HSL (Hue, Saturation, Luminance) umrechne, und umgekehrt??

die formeln aus der GraphUtil Unit funktionieren nich, und auch sehr viele die ich im Internet gefunden habe nicht...

irgendwie liefert jede Formel nen anderes Ergebniss...

Au'revoir,
Aya~

_________________
Aya
I aim for my endless dreams and I know they will come true!
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Mi 26.03.03 01:35 
Hab bloß mal kurz gestöbert und folgendes PDF gefunden, da steht sogar Delphi-Code drin, wenn ich mich arg täusche (nur kurz reingespickt).

www.oki-osd.de/downloads/004/14109-NN.pdf

Gruß
Aya Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Mi 26.03.03 01:50 
Hi,

danke... aber... ich hab in dem PDF nich einen einzigen hinweis darauf gefunden wie man HSL Farben ausrechnet ... ;)

Au'revoir,
Aya~

_________________
Aya
I aim for my endless dreams and I know they will come true!
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Mi 26.03.03 09:50 
Hmm? Ganz am Ende hinter dem Impressum kommt ein Kapitel Farbkonvertierungen Da steht dann halber Delphi-Code :wink:, ist net ganz einfach, aber so wie's aussieht macht der genau das. Habs dir jetzt aber mal rauskopiert:
Zitat:

RGB->HSL
1. Skalieren der RBG Werte auf Bereich 0-1
2. minimalen und maximalen Wert finden
3. L = (maxcolor + mincolor)/2
4. If mincolor=maxcolor dann S = 0, H undefiniert (=0)
5. else:
if L < 0.5, S=(maxcolor-mincolor)/(maxcolor+mincolor)
if L >=0.5, S=(maxcolor-mincolor)/(2.0-maxcolor-mincolor)
6. if R=maxcolor, H = (G-B)/(maxcolor-mincolor)
if G=maxcolor, H = 2.0 + (B-R)/(maxcolor-mincolor)
if B=maxcolor, H = 4.0 + (R-G)/(maxcolor-mincolor)
7. H = H*60.0;
if H < 0 dann H=H+360;
8. S=S*100; L=L*100;
Anschließend ist H der Winkel im Farbkreis, S und L geben in Prozent
die Sättigung und Leuchtkraft an.

HSL->RGB
1. if S=0, dann R=L, G=L und B=L;
2. else:
If L < 0.5, temp2=L*(1.0+S)
If L >= 0.5, temp2=L+S - L*S
temp1 = 2.0*L - temp2
3. skaliere H in den Bereich zwischen 0-1
4. für jeden der Farbwerte (R,G,B)
lege eine eigene temporäre Variable, temp3, an:
für R, temp3=H+1.0/3.0
für G, temp3=H
für B, temp3=H-1.0/3.0
if temp3 < 0, temp3 = temp3 + 1.0
if temp3 > 1, temp3 = temp3 - 1.0
5. Für je R,G,B:
if 6.0*temp3 < 1, color=temp1+(temp2-temp1)*6.0*temp3
else if 2.0*temp3 < 1, color=temp2
else if 3.0*temp3 < 2, color=temp1+(temp2-temp1)*((2.0/3.0)-
temp3)*6.0
else color=temp1
6. Zurückskalieren auf Bereich 0-256


Probier das mal.....
Matthias
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 121



BeitragVerfasst: Mi 26.03.03 11:40 
Hallo,

ich habe in meinen Bibliotheken einen "Code Schnipsel" hierfür gefunden. Die entspricht im wesentlichen der Bereschreibung des letzten Posting in Code Form.

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
procedure RGBtoHSL (R,G,B: double; var H, S, L : double);
var
  Delta,Max,
  Min: double;

begin
  R :=  R / 255;
  G :=  G / 255;
  B :=  B / 255;

  if   R > G
  then begin
    if   R > B  then Max := R  else Max := B;
    if   G < B  then Min := G  else Min := B;
  end
  else begin
      if   G > B then Max := G else Max := B;
      if   R < B then Min := R else Min := B;
  end

  // Berechne Luminosity (L)
  L := (Max + Min) / 2;

  if Max = Min then  // Wenn Max = Min dann ist es grau
  begin
     H := 0;              // -> H ist nicht definiert
     S := 0
  end else begin
    Delta := Max - Min;

    // Berechne Saturation (S)
    if L < 0.5 then  
      S := Delta / (Max + Min)
    else
      S := Delta / (2 - Max - Min);

    // Berechne Hue (H)
    if R = Max then
      H := (G - B) / Delta
    else
      if G = Max then
        H  := 2 + (B - R) /Delta
      else
        H := 4 + (R - G) / Delta;
    H := H / 6;
    if H < 0 then  H := H + 1;
  end
end;


ciao
Matthias
Simon Joker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 236
Erhaltene Danke: 1



BeitragVerfasst: Mi 26.03.03 16:11 
und hie die komplette Unit:
ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
unit HSLUtility;

interface

uses
  Windows, Graphics;

var
  HSLRange : integer = 240;

// convert a HSL value into a RGB in a TColor
// HSL values are 0.0 to 1.0 double
function HSLtoRGB (H, S, L: double): TColor;

// convert a HSL value into a RGB in a TColor
// SL values are 0 to the HSLRange variable
// H value is to HSLRange-1
function HSLRangeToRGB (H, S, L : integer): TColor;

// convert a RGB value (as TColor) into HSL
// HSL values are 0.0 to 1.0 double
procedure RGBtoHSL (RGB: TColor; var H, S, L : double);

// convert a RGB value (as TColor) into HSL
// SL values are 0 to the HSLRange variable
// H value is to HSLRange-1
procedure RGBtoHSLRange (RGB: TColor; var H, S, L : integer);

implementation

function HSLtoRGB (H, S, L: double): TColor;
var
  M1,
  M2: double;

  function HueToColourValue (Hue: double) : byte;
  var
    V : double;
  begin
    if Hue < 0 then
      Hue := Hue + 1
    else
      if Hue > 1 then
        Hue := Hue - 1;

    if 6 * Hue < 1 then
      V := M1 + (M2 - M1) * Hue * 6
    else
      if 2 * Hue < 1 then
        V := M2
      else
        if 3 * Hue < 2 then
          V := M1 + (M2 - M1) * (2/3 - Hue) * 6
        else
          V := M1;
    Result := round (255 * V)
  end;

var
  R,
  G,
  B: byte;
begin
  if S = 0 then
  begin
    R := round (255 * L);
    G := R;
    B := R
  end else begin
    if L <= 0.5 then
      M2 := L * (1 + S)
    else
      M2 := L + S - L * S;
    M1 := 2 * L - M2;
    R := HueToColourValue (H + 1/3);
    G := HueToColourValue (H);
    B := HueToColourValue (H - 1/3)
  end;

  Result := RGB (R, G, B)
end;

function HSLRangeToRGB (H, S, L : integer): TColor;
begin
  Result := HSLToRGB (H / (HSLRange-1), S / HSLRange, L / HSLRange)
end;

// Convert RGB value (0-255 range) into HSL value (0-1 values)

procedure RGBtoHSL (RGB: TColor; var H, S, L : double);

  function Max (a, b : double): double;
  begin
    if a > b then
      Result := a
    else
      Result := b
  end;

  function Min (a, b : double): double;
  begin
    if a < b then
      Result := a
    else
      Result := b
  end;

var
  R,
  G,
  B,
  D,
  Cmax,
  Cmin: double;

begin
  R := GetRValue (RGB) / 255;
  G := GetGValue (RGB) / 255;
  B := GetBValue (RGB) / 255;
  Cmax := Max (R, Max (G, B));
  Cmin := Min (R, Min (G, B));

// calculate luminosity
  L := (Cmax + Cmin) / 2;

  if Cmax = Cmin then  // it's grey
  begin
    H := 0; // it's actually undefined
    S := 0
  end else begin
    D := Cmax - Cmin;

// calculate Saturation
    if L < 0.5 then
      S := D / (Cmax + Cmin)
    else
      S := D / (2 - Cmax - Cmin);

// calculate Hue
    if R = Cmax then
      H := (G - B) / D
    else
      if G = Cmax then
        H  := 2 + (B - R) /D
      else
        H := 4 + (R - G) / D;

    H := H / 6;
    if H < 0 then
      H := H + 1
  end
end;

procedure RGBtoHSLRange (RGB: TColor; var H, S, L : integer);
var
  Hd,
  Sd,
  Ld: double;
begin
  RGBtoHSL (RGB, Hd, Sd, Ld);
  H := round (Hd * (HSLRange-1));
  S := round (Sd * HSLRange);
  L := round (Ld * HSLRange);
end;

end.