Temperaturfarbe: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 15: | Zeile 15: | ||
*im Intervall [midtemp2,endtemp] color(temp) = (1-u)² * midcolor2 + 2 * u * (1-u) * midcolor3 + u² * endcolor; dabei ist zu setzen u = (temp-midtemp2)/(endtemp-midtemp2) | *im Intervall [midtemp2,endtemp] color(temp) = (1-u)² * midcolor2 + 2 * u * (1-u) * midcolor3 + u² * endcolor; dabei ist zu setzen u = (temp-midtemp2)/(endtemp-midtemp2) | ||
= | = Skala 1 = | ||
startcolor={0,255,255}; | startcolor={0,255,255}; | ||
midcolor1={30,80,255}; | midcolor1={30,80,255}; | ||
Zeile 27: | Zeile 27: | ||
</gallery> | </gallery> | ||
= Skala 2 = | |||
= | |||
startcolor={0,255,255}; | startcolor={0,255,255}; | ||
midcolor1={120,120,120}; | midcolor1={120,120,120}; | ||
Zeile 41: | Zeile 39: | ||
</gallery> | </gallery> | ||
= | = Skala 3 = | ||
startcolor={0,69,255}; | startcolor={0,69,255}; | ||
midcolor1={120,180,180}; | midcolor1={120,180,180}; | ||
Zeile 53: | Zeile 51: | ||
</gallery> | </gallery> | ||
= Code = | |||
Nachstehend der Code für die Farbskala 1 | |||
{| class="wikitable" | |||
|- class="hintergrundfarbe6" | |||
! Perl-Code | |||
! Shell-Code | |||
|- | |||
| | |||
sub pahColor { | |||
my ($starttemp,$midtemp2,$endtemp,$temp,$opacity) = @_; | |||
$opacity //= 255; | |||
my($uval,$rval,$rval1,$rval2,$rval3); | |||
my($gval,$gval1,$gval2,$gval3); | |||
my($bval,$bval1,$bval2,$bval3); | |||
#### startcolor | |||
my $startcolorR = 0; | |||
my $startcolorG = 255; | |||
my $startcolorB = 255; | |||
### midcolor1 | |||
my $midcolor1R = 30; | |||
my $midcolor1G = 80; | |||
my $midcolor1B = 255; | |||
### midcolor2 | |||
my $midcolor2R = 40; | |||
my $midcolor2G = 255; | |||
my $midcolor2B = 60; | |||
### midcolor3 | |||
my $midcolor3R = 160; | |||
my $midcolor3G = 128; | |||
my $midcolor3B = 10; | |||
### endcolor | |||
my $endcolorR = 255; | |||
my $endcolorG = 69; | |||
my $endcolorB = 0; | |||
return sprintf("%02X%02X%02X%02X",$startcolorR,$startcolorG,$startcolorB,$opacity) | |||
if ($temp <= $starttemp); | |||
return sprintf("%02X%02X%02X%02X",$endcolorR,$endcolorG,$endcolorB,$opacity) | |||
if ($temp > $endtemp); | |||
if ($temp <= $midtemp2) { | |||
$uval = sprintf("%.5f",($temp - $starttemp) / ($midtemp2 - $starttemp)); | |||
$rval1 = sprintf("%.5f",(1-$uval)**2 * $startcolorR); | |||
$rval2 = sprintf("%.5f",2*(1-$uval) * $uval * $midcolor1R); | |||
$rval3 = sprintf("%.5f",$uval**2 * $midcolor2R); | |||
$rval = sprintf("%.0f",(100*($rval1 + $rval2 + $rval3)+0.5)/100); | |||
$gval1 = sprintf("%.5f",(1-$uval)**2 * $startcolorG); | |||
$gval2 = sprintf("%.5f",2*(1-$uval) * $uval * $midcolor1G); | |||
$gval3 = sprintf("%.5f",$uval**2 * $midcolor2G); | |||
$gval = sprintf("%.0f",(100*($gval1 + $gval2 + $gval3)+0.5)/100); | |||
$bval1 = sprintf("%.5f",(1-$uval)**2 * $startcolorB); | |||
$bval2 = sprintf("%.5f",2*(1-$uval) * $uval * $midcolor1B); | |||
$bval3 = sprintf("%.5f",$uval**2 * $midcolor2B); | |||
$bval = sprintf("%.0f",(100*($bval1 + $bval2 + $bval3)+0.5)/100); | |||
return sprintf("%02X%02X%02X%02X",$rval,$gval,$bval,$opacity); | |||
} | |||
if ($temp <= $endtemp) { | |||
$uval = sprintf("%.5f",($temp - $midtemp2)/($endtemp - $midtemp2)); | |||
$rval1 = sprintf("%.5f",(1-$uval)**2 * $midcolor2R); | |||
$rval2 = sprintf("%.5f",2 * (1-$uval) * $uval * $midcolor3R); | |||
$rval3 = sprintf("%.5f",$uval**2 * $endcolorR); | |||
$rval = sprintf("%.0f",(100*($rval1+$rval2+$rval3)+0.5)/100); | |||
$gval1 = sprintf("%.5f",(1-$uval)**2 * $midcolor2G); | |||
$gval2 = sprintf("%.5f",2 * (1-$uval) * $uval * $midcolor3G); | |||
$gval3 = sprintf("%.5f",$uval**2 * $endcolorG); | |||
$gval = sprintf("%.0f",(100*($gval1+$gval2+$gval3)+0.5)/100); | |||
$bval1 = sprintf("%.5f",(1-$uval)**2 * $midcolor2B); | |||
$bval2 = sprintf("%.5f",2*(1-$uval)*$uval*$midcolor3B); | |||
$bval3 = sprintf("%.5f",$uval**2 *$endcolorB); | |||
$bval = sprintf("%.0f",(100*($bval1+$bval2+$bval3)+0.5)/100); | |||
return sprintf("%02X%02X%02X%02X",$rval,$gval,$bval,$opacity); | |||
} | |||
} | |||
| | |||
# Color function | |||
colorfunction () { | |||
temp=$1 | |||
### startcolor | |||
startcolorR=0 | |||
startcolorG=255 | |||
startcolorB=255 | |||
### midcolor1 | |||
midcolor1R=30 | |||
midcolor1G=80 | |||
midcolor1B=255 | |||
### midcolor2 | |||
midcolor2R=40 | |||
midcolor2G=255 | |||
midcolor2B=60 | |||
### midcolor3 | |||
midcolor3R=160 | |||
midcolor3G=128 | |||
midcolor3B=10 | |||
### endcolor | |||
endcolorR=255 | |||
endcolorG=69 | |||
endcolorB=0 | |||
starttemp=15 | |||
midtemp1=18 | |||
midtemp2=21 | |||
midtemp3=24 | |||
endtemp=27 | |||
if [ $(bc <<< "$temp <= $starttemp") -eq 1 ] | |||
then | |||
retval="rgba($startcolorR,$startcolorG,$startcolorB,$opacity)" | |||
else | |||
if [ $(bc <<< "$temp <= $midtemp2") -eq 1 ] | |||
then | |||
uval=`echo "scale=5;($temp - $starttemp)/($midtemp2 - $starttemp)" | bc` | |||
rval1=`echo "scale=5;(1-$uval)^2*$startcolorR" | bc` | |||
rval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor1R" | bc` | |||
rval3=`echo "scale=5;$uval^2*$midcolor2R" | bc` | |||
rval=`echo "scale=0;(100*($rval1+$rval2+$rval3)+0.5)/100" | bc` | |||
gval1=`echo "scale=5;(1-$uval)^2*$startcolorG" | bc` | |||
gval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor1G" | bc` | |||
gval3=`echo "scale=5;$uval^2*$midcolor2G" | bc` | |||
gval=`echo "scale=0;(100*($gval1+$gval2+$gval3)+0.5)/100" | bc` | |||
bval1=`echo "scale=5;(1-$uval)^2*$startcolorB" | bc` | |||
bval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor1B" | bc` | |||
bval3=`echo "scale=5;$uval^2*$midcolor2B" | bc` | |||
bval=`echo "scale=0;(100*($bval1+$bval2+$bval3)+0.5)/100" | bc` | |||
retval="rgba($rval,$gval,$bval,$opacity)" | |||
else | |||
if [ $(bc <<< "$temp <= $endtemp") -eq 1 ] | |||
then | |||
uval=`echo "scale=5;($temp - $midtemp2)/($endtemp - $midtemp2)" | bc` | |||
rval1=`echo "scale=5;(1-$uval)^2*$midcolor2R" | bc` | |||
rval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor3R" | bc` | |||
rval3=`echo "scale=5;$uval^2*$endcolorR" | bc` | |||
rval=`echo "scale=0;(100*($rval1+$rval2+$rval3)+0.5)/100" | bc` | |||
gval1=`echo "scale=5;(1-$uval)^2*$midcolor2G" | bc` | |||
gval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor3G" | bc` | |||
gval3=`echo "scale=5;$uval^2*$endcolorG" | bc` | |||
gval=`echo "scale=0;(100*($gval1+$gval2+$gval3)+0.5)/100" | bc` | |||
bval1=`echo "scale=5;(1-$uval)^2*$midcolor2B" | bc` | |||
bval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor3B" | bc` | |||
bval3=`echo "scale=5;$uval^2*$endcolorB" | bc` | |||
bval=`echo "scale=0;(100*($bval1+$bval2+$bval3)+0.5)/100" | bc` | |||
retval="rgba($rval,$gval,$bval,$opacity)" | |||
else | |||
retval="rgba($endcolorR,$endcolorG,$endcolorB,$opacity)" | |||
fi | |||
fi | |||
fi | |||
echo $retval | |||
} | |||
|} | |||
[[Kategorie:Code Snippets]] | [[Kategorie:Code Snippets]] |
Version vom 14. Februar 2015, 18:19 Uhr
Achtung, diese Seite behandelt nicht die Farbtemperatur.
Das Ziel dieser Seite ist die Bereitstellung einer Funktion, mit der man eine bestimmte Temperatur in einen Farbwert umwandeln kann. Dazuist die Farbtemperatur eher ungeeignet.
Also bedienen wir uns einer Bézier-Approximation. Dieses in der Computergrafik sehr häufig verwendete Verfahren ist benannt nach Pierre Bézier und diente schon in den 1960er Jahren zum Design von Autokarosserien von Renault. Unsere Skala wird durch drei Temperaturen bestimmt:
- starttemp ist die untere Skalengrenze, ihr zugeordnet ist eine Wunschfarbe startcolor
- midtemp2 ist die ideale Temperatur, ihr zugeordnet ist eine Wunschtemperatur midcolor2
- endtemp ist die obere Skalengrenze, ihr zugeordnet ist eine Wunschfarbe endcolor
Zusätzlich benötigen wir zwei Kontrollpunkte im Farbraum,
- midcolor1
- midcolor3
Die Farbskala ergibt sich dann, indem zwei quadratische Bézier-Segmente aneinandergesetzt werden.
- Im Intervall [starttemp,midtemp2] color(temp) = (1-u)² * startcolor + 2 * u * (1-u) * midcolor1 + u² * midcolor2; dabei ist zu setzen u = (temp-starttemp)/(midtemp2-starttemp)
- im Intervall [midtemp2,endtemp] color(temp) = (1-u)² * midcolor2 + 2 * u * (1-u) * midcolor3 + u² * endcolor; dabei ist zu setzen u = (temp-midtemp2)/(endtemp-midtemp2)
Skala 1
startcolor={0,255,255}; midcolor1={30,80,255}; midcolor2={40,255,60}; midcolor3={160,128,10}; endcolor={255,69,0};
Das führt zu der Temperaturskala
Skala 2
startcolor={0,255,255}; midcolor1={120,120,120}; midcolor2={40,255,60}; midcolor3={255,255,0}; endcolor={255,69,0};
Das führt zu der Temperaturskala
Skala 3
startcolor={0,69,255}; midcolor1={120,180,180}; midcolor2={40,255,60}; midcolor3={255,255,0}; endcolor={255,69,0};
Das führt zu der Temperaturskala
Code
Nachstehend der Code für die Farbskala 1
Perl-Code | Shell-Code |
---|---|
sub pahColor { my ($starttemp,$midtemp2,$endtemp,$temp,$opacity) = @_; $opacity //= 255; my($uval,$rval,$rval1,$rval2,$rval3); my($gval,$gval1,$gval2,$gval3); my($bval,$bval1,$bval2,$bval3); #### startcolor my $startcolorR = 0; my $startcolorG = 255; my $startcolorB = 255; ### midcolor1 my $midcolor1R = 30; my $midcolor1G = 80; my $midcolor1B = 255; ### midcolor2 my $midcolor2R = 40; my $midcolor2G = 255; my $midcolor2B = 60; ### midcolor3 my $midcolor3R = 160; my $midcolor3G = 128; my $midcolor3B = 10; ### endcolor my $endcolorR = 255; my $endcolorG = 69; my $endcolorB = 0; return sprintf("%02X%02X%02X%02X",$startcolorR,$startcolorG,$startcolorB,$opacity) if ($temp <= $starttemp); return sprintf("%02X%02X%02X%02X",$endcolorR,$endcolorG,$endcolorB,$opacity) if ($temp > $endtemp); if ($temp <= $midtemp2) { $uval = sprintf("%.5f",($temp - $starttemp) / ($midtemp2 - $starttemp)); $rval1 = sprintf("%.5f",(1-$uval)**2 * $startcolorR); $rval2 = sprintf("%.5f",2*(1-$uval) * $uval * $midcolor1R); $rval3 = sprintf("%.5f",$uval**2 * $midcolor2R); $rval = sprintf("%.0f",(100*($rval1 + $rval2 + $rval3)+0.5)/100); $gval1 = sprintf("%.5f",(1-$uval)**2 * $startcolorG); $gval2 = sprintf("%.5f",2*(1-$uval) * $uval * $midcolor1G); $gval3 = sprintf("%.5f",$uval**2 * $midcolor2G); $gval = sprintf("%.0f",(100*($gval1 + $gval2 + $gval3)+0.5)/100); $bval1 = sprintf("%.5f",(1-$uval)**2 * $startcolorB); $bval2 = sprintf("%.5f",2*(1-$uval) * $uval * $midcolor1B); $bval3 = sprintf("%.5f",$uval**2 * $midcolor2B); $bval = sprintf("%.0f",(100*($bval1 + $bval2 + $bval3)+0.5)/100); return sprintf("%02X%02X%02X%02X",$rval,$gval,$bval,$opacity); } if ($temp <= $endtemp) { $uval = sprintf("%.5f",($temp - $midtemp2)/($endtemp - $midtemp2)); $rval1 = sprintf("%.5f",(1-$uval)**2 * $midcolor2R); $rval2 = sprintf("%.5f",2 * (1-$uval) * $uval * $midcolor3R); $rval3 = sprintf("%.5f",$uval**2 * $endcolorR); $rval = sprintf("%.0f",(100*($rval1+$rval2+$rval3)+0.5)/100); $gval1 = sprintf("%.5f",(1-$uval)**2 * $midcolor2G); $gval2 = sprintf("%.5f",2 * (1-$uval) * $uval * $midcolor3G); $gval3 = sprintf("%.5f",$uval**2 * $endcolorG); $gval = sprintf("%.0f",(100*($gval1+$gval2+$gval3)+0.5)/100); $bval1 = sprintf("%.5f",(1-$uval)**2 * $midcolor2B); $bval2 = sprintf("%.5f",2*(1-$uval)*$uval*$midcolor3B); $bval3 = sprintf("%.5f",$uval**2 *$endcolorB); $bval = sprintf("%.0f",(100*($bval1+$bval2+$bval3)+0.5)/100); return sprintf("%02X%02X%02X%02X",$rval,$gval,$bval,$opacity); } } |
# Color function colorfunction () { temp=$1 ### startcolor startcolorR=0 startcolorG=255 startcolorB=255 ### midcolor1 midcolor1R=30 midcolor1G=80 midcolor1B=255 ### midcolor2 midcolor2R=40 midcolor2G=255 midcolor2B=60 ### midcolor3 midcolor3R=160 midcolor3G=128 midcolor3B=10 ### endcolor endcolorR=255 endcolorG=69 endcolorB=0 starttemp=15 midtemp1=18 midtemp2=21 midtemp3=24 endtemp=27 if [ $(bc <<< "$temp <= $starttemp") -eq 1 ] then retval="rgba($startcolorR,$startcolorG,$startcolorB,$opacity)" else if [ $(bc <<< "$temp <= $midtemp2") -eq 1 ] then uval=`echo "scale=5;($temp - $starttemp)/($midtemp2 - $starttemp)" | bc` rval1=`echo "scale=5;(1-$uval)^2*$startcolorR" | bc` rval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor1R" | bc` rval3=`echo "scale=5;$uval^2*$midcolor2R" | bc` rval=`echo "scale=0;(100*($rval1+$rval2+$rval3)+0.5)/100" | bc` gval1=`echo "scale=5;(1-$uval)^2*$startcolorG" | bc` gval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor1G" | bc` gval3=`echo "scale=5;$uval^2*$midcolor2G" | bc` gval=`echo "scale=0;(100*($gval1+$gval2+$gval3)+0.5)/100" | bc` bval1=`echo "scale=5;(1-$uval)^2*$startcolorB" | bc` bval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor1B" | bc` bval3=`echo "scale=5;$uval^2*$midcolor2B" | bc` bval=`echo "scale=0;(100*($bval1+$bval2+$bval3)+0.5)/100" | bc` retval="rgba($rval,$gval,$bval,$opacity)" else if [ $(bc <<< "$temp <= $endtemp") -eq 1 ] then uval=`echo "scale=5;($temp - $midtemp2)/($endtemp - $midtemp2)" | bc` rval1=`echo "scale=5;(1-$uval)^2*$midcolor2R" | bc` rval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor3R" | bc` rval3=`echo "scale=5;$uval^2*$endcolorR" | bc` rval=`echo "scale=0;(100*($rval1+$rval2+$rval3)+0.5)/100" | bc` gval1=`echo "scale=5;(1-$uval)^2*$midcolor2G" | bc` gval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor3G" | bc` gval3=`echo "scale=5;$uval^2*$endcolorG" | bc` gval=`echo "scale=0;(100*($gval1+$gval2+$gval3)+0.5)/100" | bc` bval1=`echo "scale=5;(1-$uval)^2*$midcolor2B" | bc` bval2=`echo "scale=5;2*(1-$uval)*$uval*$midcolor3B" | bc` bval3=`echo "scale=5;$uval^2*$endcolorB" | bc` bval=`echo "scale=0;(100*($bval1+$bval2+$bval3)+0.5)/100" | bc` retval="rgba($rval,$gval,$bval,$opacity)" else retval="rgba($endcolorR,$endcolorG,$endcolorB,$opacity)" fi fi fi echo $retval } |