Zum Hauptinhalt springen
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

Beispiel - HLS-Farbwerte

Beispiel - HLS-Farbwerte Umrechnung von Farbwerten in das HLS-Modell

define
{
mUndefined : -1.0
mValue2Float(a) : CnvFI(a) / 255.0
mFloat2Value(a) : CnvIF(a * 255.0)
}

// *********************************************************************
// ***** *****
// ***** Rgb2Hls(var array1, var array2) *****
// ***** *****
// ***** array1 : Array bestehend aus drei float-Werten. Die *****
// ***** Werte repräsentieren die Farbanteile rot, *****
// ***** grün und blau jeweils in Werten von [0..1] *****
// ***** *****
// ***** array2 : Array bestehend aus drei float-Werten. Die *****
// ***** Werte repräsentieren die Anteile Hue (Farbe), *****
// ***** Lumination (Helligkeit) und Saturation *****
// ***** (Sättigung) jeweils in Werten von [0..1] *****
// ***** *****
// ***** Das Ergebnis der Umrechnung wird in (array2) abgelegt. *****
// ***** *****
// *********************************************************************

sub Rgb2Hls
(
var aRgbColor : float[]; // in: Farbwerte in RGB-Darstellung
var aHlsColor : float[]; // out: Farbwerte in HLS-Darstellung
)

local
{
tMax : float;
tMin : float;
tDelta : float;
}

{
tMax # Max(aRgbColor[1], Max(aRgbColor[2], aRgbColor[3]));
tMin # Min(aRgbColor[1], Min(aRgbColor[2], aRgbColor[3]));
aHlsColor[2] # (tMax + tMin) / 2.0; // Helligkeit

if (tMax = tMin)
{
// Berechnung der Farbe und der Sättigung für s/w Darstellung
aHlsColor[1] # mUndefined;
aHlsColor[3] # 0.0;
}
else
{
// Berechnung der Sättigung für Farbdarstellung
if (aHlsColor[2] <= 0.5)
aHlsColor[3] # (tMax - tMin) / (tMax + tMin);
else
aHlsColor[3] # (tMax - tMin) / (2.0 - tMax - tMin);

// Berechnung der Farbe für Farbdarstellung
tDelta # tMax - tMin;
switch (true)
{
case (aRgbColor[1] = tMax) : aHlsColor[1] # (aRgbColor[2] - aRgbColor[3]) / tDelta;

case (aRgbColor[2] = tMax) : aHlsColor[1] # 2.0 + (aRgbColor[3] - aRgbColor[1]) / tDelta;
case (aRgbColor[3] = tMax) : aHlsColor[1] # 4.0 + (aRgbColor[1] - aRgbColor[2]) / tDelta;
}

aHlsColor[1] # aHlsColor[1] * 60.0;
if (aHlsColor[1] < 0.0)
aHlsColor[1] # aHlsColor[1] + 360.0;
}
}

// *********************************************************************
// ***** *****
// ***** Value(float1, float2, float3) : float *****
// ***** *****
// ***** Aus den Werten (float1) bis (float3) wird einer der RGB- *****
// ***** Anteile berechnet. *****
// ***** *****
// *********************************************************************

sub Value
(
an1 : float;
an2 : float;
ahue : float;
) : float;

{
// Wertebereich überprüfen und angleichen
if (ahue > 360.0)
ahue # ahue - 360.0;
if (ahue < 0.0)
ahue # ahue + 360.0;

// Farbwert berechnen
switch (true)
{
case (ahue < 60.0) : return(n1 + (an2 - an1) * ahue / 60.0);
case (ahue < 180.0) : return(n2);
case (ahue < 240.0) : return(n1 + (an2 - an1) * (240.0 - ahue) / 60.0);
default : return(an1);
}
}

// *********************************************************************
// ***** *****
// ***** Hls2Rgb(var array1, var array2) *****
// ***** *****
// ***** array1 : Array bestehend aus drei float-Werten. Die *****
// ***** Werte repräsentieren die Anteile Hue (Farbe), *****
// ***** Lumination (Helligkeit) und Saturation *****
// ***** (Sättigung) jeweils in Werten von [0..1] *****
// ***** *****
// ***** array2 : Array bestehend aus drei float-Werten. Die *****
// ***** Werte repräsentieren die Farbanteile rot, *****
// ***** grün und blau jeweils in Werten von [0..1] *****
// ***** *****
// ***** Das Ergebnis der Umrechnung wird in (array2) abgelegt. *****
// ***** *****
// *********************************************************************

sub Hls2Rgb
(
var aHlsColor : float[]; // in: Farbwerte in HLS-Darstellung
var aRgbColor : float[]; // out: Farbwerte in RGB-Darstellung
)

local
{
tm1 : float;
tm2 : float;
}

{
if (aHlsColor[2] <= 0.5)
tm2 # aHlsColor[2] * (1.0 + aHlsColor[3]);
else
tm2 # aHlsColor[2] + aHlsColor[3] - (aHlsColor[2] * aHlsColor[3]);
tm1 # (2.0 * aHlsColor[2]) - tm2;
if (aHlsColor[3] = 0.0)
{
if (aHlsColor[1] = mUndefined)
{
// s/w Darstellung
aRgbColor[1] # aHlsColor[2];
aRgbColor[2] # aHlsColor[2];
aRgbColor[3] # aHlsColor[2];
}
}
else
{
// Farb-Darstellung
aRgbColor[1] # Value(tm1, tm2, aHlsColor[1] + 120.0);
aRgbColor[2] # Value(tm1, tm2, aHlsColor[1]);
aRgbColor[3] # Value(tm1, tm2, aHlsColor[1] - 120.0);
}
}