| | | | |
| | |
| PHP Code Flüsterer Registriert seit: 21.08.2005 Beiträge: 4682 PHP-Kenntnisse: Fortgeschritten | |
| | |
| Gast
Beiträge: n/a
| jo, Lösung: man brauch 3 Funktionen: Die 1. wandelt eine HEX-Farbe in einen RGB-Wert um. Die 2. wandelt den RGB-Wert in einen HSV-Wert um An diesem HSV-Wert kann man dann die Satuation/Value so anpassen, das eine schöne nuance entsteht Die 3. Funktion wandelt den HSV-Wert wieder zurück in RGB. Die Funktionen zum umwandeln hab ich von http://www.zend.com/codex.php?id=1403&single=1 Wens interessiert Code: <?
/**
* Convert a hex colour string into an rgb array.
*
* Handles colour string in the following formats:
*
* o #44FF55
* o 4FF55
* o #4F5
* o 4F5
*
* @return array
* @param string $hex
* @access public
*/
function hex2rgb($hex)
{
$hex = @preg_replace('/^#/', '', $hex);
if (strlen($hex) == 3) {
$v = explode(':', chunk_split($hex, 1, ':'));
return array(16 * hexdec($v[0]) + hexdec($v[0]), 16 * hexdec($v[1]) + hexdec($v[1]), 16 * hexdec($v[2]) + hexdec($v[2]));
} else {
$v = explode(':', chunk_split($hex, 2, ':'));
return array(hexdec($v[0]), hexdec($v[1]), hexdec($v[2]));
}
}
/**
* Convert an rgb array into a hex colour string.
*
* Handles colour string in the following formats:
*
* o #44FF55
* o 4FF55
* o #4F5
* o 4F5
*
* @return array
* @param string $hex
* @access public
*/
function rgb2hex($rgb, $adHash = true)
{
return sprintf("%s%02X%02X%02X", ($adHash ? '#' : ''), $rgb[0], $rgb[1], $rgb[2]);
}
/**
* Convert an RGB array into HLS colour space.
*
* Expects array(r, g, b) where r, g, b in [0,255]. The HLS array is
* returned as array(h, l, s) where h is in [0,360], l and s in [0,1].
*
* Function adapted from 'Computer Graphics: Principles and Practice',
* by Foley, van Dam, Feiner and Hughes. Chapter 13; Achromatic and
* Colored Light.
*
* @return array
* @param array $rgb
* @access public
*/
function rgb2hls($rgb)
{
for ($c=0; $c<3; $c++) {
$rgb[$c] = $rgb[$c] / 255;
}
$hls = array(0, 0, 0);
$max = max($rgb);
$min = min($rgb);
$hls[1] = ($max + $min) / 2;
if ($max == $min) {
$hls[0] = null;
$hls[2] = 0;
} else {
$delta = $max - $min;
$hls[2] = ($hls[1] <= 0.5) ? ($delta / ($max + $min)) : ($delta / (2 - $delta));
if ($rgb[0] == $max) {
$hls[0] = ($rgb[1] - $rgb[2]) / $delta;
} else if ($rgb[1] == $max) {
$hls[0] = 2 + ($rgb[2] - $rgb[0]) / $delta;
} else {
$hls[0] = 4 + ($rgb[0] - $rgb[1]) / $delta;
}
$hls[0] *= 60;
if ($hls[0] < 0) {
$hls[0] += 360;
}
if ($hls[0] > 360) {
$hls[0] -= 360;
}
}
ksort($hls);
return $hls;
}
/**
* Convert HLS colour space array to RGB colour space.
*
* Expects HLS array as array(h, l, s) where h in [0,360], l and s each
* in [0,1]. Returns array(r, g, b) where r, g, and b each in [0, 255]
*
* Function adapted from 'Computer Graphics: Principles and Practice',
* by Foley, van Dam, Feiner and Hughes. Chapter 13; Achromatic and
* Colored Light.
*
* @return array
* @param array $hls
* @access public
*/
function hls2rgb($hls)
{
$rgb = array(0, 0, 0);
$m2 = ($hls[1] <= 0.5) ? ($hls[1] * (1 + $hls[2])) : ($hls[1] + $hls[2] * (1 - $hls[1]));
$m1 = 2 * $hls[1] - $m2;
if (!$hls[2]) {
if ($hls[0] === null) {
$rgb[0] = $rgb[1] = $rgb[2] = $hls[1];
} else {
return false;
}
} else {
$rgb[0] = _hVal($m1, $m2, $hls[0] + 120);
$rgb[1] = _hVal($m1, $m2, $hls[0]);
$rgb[2] = _hVal($m1, $m2, $hls[0] - 120);
}
for ($c=0; $c<3; $c++) {
$rgb[$c] = round($rgb[$c] * 255);
}
return $rgb;
}
/**
* Hue value checker for HSL colour space routine.
*
* @return float
* @param float $n1
* @param float $n2
* @param float $h
* @access private
* @see Image::hls2rgb()
*/
function _hVal($n1, $n2, $h)
{
if ($h > 360) {
$h -= 360;
} else if ($h < 0) {
$h += 360;
}
if ($h < 60) {
return $n1 + ($n2 - $n1) * $h / 60;
} else if ($h < 180) {
return $n2;
} else if ($h < 240) {
return $n1 + ($n2 - $n1) * (240 - $h) / 60;
} else {
return $n1;
}
}
/*
Funktion zum erstellen einer Farbnuance einer HEX-Farbe
@return string
@param string $color
@access private
*/
function create_nuance($color)
{
// Hex-Farbe in RGB und RGB in HSV umwandeln
$hsvcolor = rgb2hls(hex2rgb($color));
// Satuartion der Farbe erhöhen
$hsvcolor[1] = $hsvcolor[1]+0.3;
// HSV zurück nach RGB wandeln und RGB zurück nach GEX
return rgb2hex(hls2rgb($hsvcolor));
}
?>
ben |
| Themen-Optionen | |
| Thema bewerten | |
|
|
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Durschnitt berechnen | schimanski | Datenbanken | 6 | 04.06.2008 15:47 |
| Arbeitstage berechnen (Mo-Fr) | bp158 | PHP Tipps 2008 | 9 | 06.10.2007 13:20 |
| entfernung mittels plz berechnen | kid01 | PHP-Fortgeschrittene | 10 | 13.10.2006 17:53 |
| [Erledigt] Sporttabelle aus Ergebnissen berechnen | Datenbanken | 6 | 13.03.2006 20:37 | |
| [Erledigt] Distanz zwischen zwei Bild-Koordinaten berechnen - Machbar? | PHP-Fortgeschrittene | 4 | 28.08.2005 17:29 | |
| hi, uhrzeiten subtrahieren und differenz berechnen | Beitragsarchiv | 0 | 04.07.2005 15:36 | |
| Schriftbreite berechnen | PHP Tipps 2005-2 | 11 | 13.06.2005 07:48 | |
| menschenalter berechnen | janni | PHP Tipps 2005 | 6 | 25.05.2005 00:13 |
| [Erledigt] Restzeit berechnen | PHP Tipps 2005 | 10 | 22.04.2005 13:02 | |
| Mathe-problem: Sinus mit beliebiger Genauigkeit berechnen? | PHP Tipps 2005 | 4 | 19.01.2005 18:10 | |
| kapazität berechnen | PHP Tipps 2005 | 7 | 10.01.2005 20:25 | |
| Help, Zahlen aus DB berechnen | PHP Tipps 2004-2 | 6 | 29.12.2004 17:55 | |
| KFZ Haftpflicht Versicherungsprämie berechnen | Beitragsarchiv | 3 | 13.12.2004 09:12 | |
| Phi berechnen - Euler Funktion | Beitragsarchiv | 6 | 02.12.2004 07:38 | |
| mit PHP traffic berechnen oder schätzen | PHP Tipps 2004 | 2 | 21.06.2004 12:59 | |
| Besucher kamen über folgende Suchanfragen bei Google auf diese Seite |
| php farben berechnen, farbton berechnen, farbnuancen, farbnuancen berechnen, php rgb to hsv, hsv farbe berechnen, hsv berechnen, farben berechnen php, farbnuance, php farbe berechnen, php hsv to rgb, hsv to rgb php, hls2rgb php, farbe berechnen php, color hsv delta, php rgb berechnen, php rgb hsv, hsv rgb berechnung, php berechnung farben, farbschema berechnen php |

Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.