funktion in helpers.php ausgelagert
This commit is contained in:
@@ -1,4 +1,62 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Konvertiert einen Hex-Farbcode in ein RGB-Format.
|
||||
*
|
||||
* @param string $hex Der Hex-Farbcode (z.B. "#FFFFFF" oder "FFF").
|
||||
* @return string Die RGB-Werte als String "r, g, b".
|
||||
*/
|
||||
function hexToRgb($hex)
|
||||
{
|
||||
$hex = ltrim($hex, '#');
|
||||
if (strlen($hex) === 3) {
|
||||
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
|
||||
}
|
||||
if (strlen($hex) !== 6) {
|
||||
return '0, 0, 0';
|
||||
}
|
||||
$r = hexdec(substr($hex, 0, 2));
|
||||
$g = hexdec(substr($hex, 2, 2));
|
||||
$b = hexdec(substr($hex, 4, 2));
|
||||
return "$r, $g, $b";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt die am besten lesbare Textfarbe (Schwarz oder Weiß) basierend auf der Helligkeit einer Hintergrundfarbe.
|
||||
*
|
||||
* @param string $hex_code Der Hex-Farbcode der Hintergrundfarbe.
|
||||
* @return string Die empfohlene Textfarbe ("#000000" oder "#FFFFFF").
|
||||
*/
|
||||
function get_readable_text_color($hex_code)
|
||||
{
|
||||
// Entferne das # falls vorhanden
|
||||
$hex_code = ltrim($hex_code, '#');
|
||||
|
||||
// Erweitere 3-stellige Hex-Codes auf 6 Stellen
|
||||
if (strlen($hex_code) == 3) {
|
||||
$hex_code = $hex_code[0] . $hex_code[0] . $hex_code[1] . $hex_code[1] . $hex_code[2] . $hex_code[2];
|
||||
}
|
||||
|
||||
// Konvertiere Hex zu RGB
|
||||
$r = hexdec(substr($hex_code, 0, 2));
|
||||
$g = hexdec(substr($hex_code, 2, 2));
|
||||
$b = hexdec(substr($hex_code, 4, 2));
|
||||
|
||||
// Berechne die relative Luminanz (Helligkeit)
|
||||
// Formel basierend auf W3C-Empfehlungen für die wahrgenommene Helligkeit
|
||||
$luminance = (0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255;
|
||||
|
||||
// Gib Weiß (#FFFFFF) für dunkle Farben und Schwarz (#000000) für helle Farben zurück
|
||||
return $luminance > 0.5 ? '#000000' : '#FFFFFF';
|
||||
}
|
||||
|
||||
/**
|
||||
* Verdunkelt einen Hex-Farbcode um einen bestimmten Betrag.
|
||||
*
|
||||
* @param string $hex Der Hex-Farbcode (z.B. "#FFFFFF" oder "FFFFFF").
|
||||
* @param int $darken_amount Der Betrag, um den die Farbe verdunkelt wird (Standard: 40).
|
||||
* @return string Der verdunkelte Hex-Farbcode.
|
||||
*/
|
||||
function darken_color($hex, $darken_amount = 40)
|
||||
{
|
||||
$hex = str_replace('#', '', $hex);
|
||||
|
||||
Reference in New Issue
Block a user