Mit Hilfe der folgenden PHP- und JavaScript-Schnipsel können hexadezimale Farbangaben in ihr RGB-Äquivalent umgewandelt werden und umgekehrt. Die Quelltexte sind, meiner Meinung nach, ausreichend dokumentiert und bedürfen somit keiner weiteren Erklärung. Wer Interesse hat, kann den nachfolgenden Quelltext gerne unter Berücksichtigung der GNU AGPL weiterverwenden und/oder erweitern.
Auch noch interessant: Liste webkompatibler Farbnamen, Farben mittels PHP invertieren
/**
* Eine kleine JavaScript-Klasse zur Umwandlung von Farben.
*
* @author Pascal Hollenstein <webmaster@zockerade.com>
* @version 1.0
* @license GNU AGPL
*/
function Color() {
/**
* Wandelt eine Farbe in hexadezimaler Schreibweise in
* ihr RGB-Äquivalent um.
*
* Beispiel:
*
* new Color().fromHexToRGB("#808080")
*
* Rückgabe:
*
* Array
* (
* [0] => 128
* [1] => 128
* [2] => 128
* )
*
* @access public
* @param string $hexColor
* @return mixed
*
* @see isHex()
*/
this.fromHexToRGB = function(hexColor) {
if (typeof hexColor == "string") {
hexColor = hexColor.substr(1);
if (isHex(hexColor) && hexColor.length == 6) {
var rgb = [];
for (var i = 0; i < 3; i++) {
rgb.push(parseInt(hexColor.substr(2 * i, 2), 16));
}
return rgb;
}
}
return false;
}
/**
* Wandelt eine RGB-Farbangabe in ihr Äquivalent in hexadezimaler
* Schreibweise um.
*
* Beispiel:
*
* new Color().fromRGBToHex(
* [
* 128,
* 128,
* 128
* ]
* )
*
* Rückgabe:
*
* string(7) "#808080"
*
* @access public
* @param array $RGBColor
* @return mixed
*
* @see isRGB()
*/
this.fromRGBToHex = function(RGBColor) {
if (typeof RGBColor == "object" && isRGB(RGBColor) == true) {
var RGBColorParts = [];
for (var i = 0; i < 3; i++) {
RGBColorParts.push(parseInt(RGBColor[i]).toString(16));
}
return "#" + RGBColorParts.join("");
}
return false;
}
/**
* Diese Funktion überprüft, ob die angegebene Zahl auch wirklich
* eine Hexadezimalzahl ist.
*
* Beispiel:
*
* isHex("#AABBZZ")
*
* Rückgabe:
*
* boolean(false)
*
* @access private
* @param string $hex
* @return boolean
*/
var isHex = function(hex) {
return parseInt(hex, 16).toString(16) === hex.toLowerCase()
}
/**
* Diese Funktion überprüft, ob das angegebene Array mit den
* RGB-Werten auch tatsächlich gültig ist.
*
* Beispiel:
*
* isRGB(
* [
* 255,
* 255,
* 256
* ]
* )
*
* Rückgabe:
*
* boolean(false)
*
* @access private
* @param array $rgb
* @return boolean
*/
var isRGB = function(rgb) {
if (0 in rgb && 1 in rgb && 2 in rgb) {
var counter = 0;
for (var i = 0; i < 3; i++) {
if (rgb[i] >= 0 && rgb[i] <= 255) {
counter++;
}
}
}
return counter == 3 ? true : false;
}
}
/**
* A small class for converting hexadecimal color codes into their rgb
* equivalents and vice versa.
*
* @author Pascal Hollenstein <webmaster@zockerade.com>
* @version 1.0
* @license GNU AGPL
*/
class color {
/**
* Converts the given hexadecimal color code into its rgb equivalent.
*
* Example:
*
* color::hex_to_rgb("#fbfbfb")
*
* Return:
*
* Array
* (
* [0] => 251
* [1] => 251
* [2] => 251
* )
*
* @access public
* @param string $hex_code
* @return mixed
*
* @see is_valid_hex_code()
*/
public static function hex_to_rgb($hex_code) {
if (is_string($hex_code) && self::is_valid_hex_code($hex_code) === true) {
$hex_code = str_replace("#", null, $hex_code);
if (strlen($hex_code) === 3) {
return array(
hexdec(substr($hex_code, 0, 1).substr($hex_code, 0, 1)),
hexdec(substr($hex_code, 1, 1).substr($hex_code, 1, 1)),
hexdec(substr($hex_code, 2, 1).substr($hex_code, 2, 1))
);
} else if (strlen($hex_code) === 6) {
return array(
hexdec(substr($hex_code, 0, 2)),
hexdec(substr($hex_code,2,2)),
hexdec(substr($hex_code,4,2))
);
}
}
return false;
}
/**
* Converts the given rgb color code into its hexadecimal equivalent.
*
* Example:
*
* color::rgb_to_hex(
* array(
* 251,
* 251,
* 251
* )
* )
*
* Return:
*
* string(7) "#fbfbfb"
*
* @access public
* @param array $rgb_code
* @return mixed
*
* @see is_valid_rgb_code()
*/
public static function rgb_to_hex($rgb_code) {
if (is_array($rgb_code) && self::is_valid_rgb_code($rgb_code) === true) {
return "#"
.str_pad(dechex($rgb_code[0]), 2, "0", STR_PAD_LEFT)
.str_pad(dechex($rgb_code[1]), 2, "0", STR_PAD_LEFT)
.str_pad(dechex($rgb_code[2]), 2, "0", STR_PAD_LEFT);
}
return false;
}
/**
* Checks if the given rgb color code is valid or not.
*
* Example:
*
* self:is_valid_rgb_code(
* array(
* 255,
* 255,
* 256
* )
* )
*
* Return:
*
* bool(false)
*
* @access private
* @param array $rgb_code
* @return boolean
*/
private static function is_valid_rgb_code($rgb_code) {
$index = 0;
foreach ($rgb_code as $key => $value) {
if ($key !== $index || $value < 0 || $value > 255 || is_int($value) === false) {
break;
}
$index++;
}
return $index === 3 ? true : false;
}
/**
* Checks if the given hexadecimal color code is valid or not.
*
* Example:
*
* self::is_valid_hex_code("#AABBZZ")
*
* Return:
*
* bool(false)
*
* @access private
* @param string $hex_code
* @return boolean
*/
private static function is_valid_hex_code($hex_code) {
if (strlen($hex_code) === 4 || strlen($hex_code) === 7) {
return preg_match('~^#[a-f0-9]{3,6}$~i', $hex_code) === 1;
}
return false;
}
}