📄 color_helper.php
字号:
"SlateGray" => array(112,128,144),
"Snow" => array(255,250,250),
"SpringGreen" => array(0,255,127),
"SteelBlue" => array(70,130,180),
"Tan" => array(210,180,140),
"Teal" => array(0,128,128),
"Thistle" => array(216,191,216),
"Tomato" => array(255,99,71),
"Turquoise" => array(64,224,208),
"Violet" => array(238,130,238),
"Wheat" => array(245,222,179),
"White" => array(255,255,255),
"WhiteSmoke" => array(245,245,245),
"Yellow" => array(255,255,0),
"YellowGreen" => array(154,205,50)
);
/**
* Translates a userdefined color specification into an array of RGB integer values.
*
* Several formats can be handled. HTML like hexadecimal colors like #f0ff00,
* names colors, arrays of RGB integer values and strings with percentage values
* like %100,%50,%20. If the format is unknown black gets returned [0, 0, 0].
*
* @var mixed Color in various formats: #f0f0f0, %20,%100,%0,
* named - black, white..., [int 0 - 255, int 0 - 255, int 0 - 255]
* @return array RGB color [int red, int green, int blue]
* @access public
* @see $colornames, HTMLColor2RGB(), PercentageColor2RGB(), NamedColor2RGB()
*/
function color2RGB($color) {
if (is_array($color)) {
// looks good...
if (3 == count($color)) {
// check the range
foreach ($color as $k => $v) {
if ($v < 0)
$color[$k] = 0;
else if ($v > 255)
$color[$k] = 255;
else
$color[$k] = (int)$v;
}
return $color;
}
// unknown format - return black
return array(0, 0 , 0);
}
// #f0f0f0
if ("#" == $color{0})
return $this->HTMLColor2RGB($color);
// %50,%100,%50
if ("%" == $color{0})
return $this->PercentageColor2RGB($color);
// might be a color name
return $this->NamedColor2RGB($color);
} // end func color2RGB
/**
* Allocates a color in the given image.
*
* Userdefined color specifications get translated into
* an array of rgb values.
*
* @param resource Image handle
* @param mixed (Userdefined) color specification
* @return resource Image color handle
* @see color2RGB()
* @access public
*/
function allocateColor(&$img, $color) {
$color = $this->color2RGB($color);
return ImageColorAllocate($img, $color[0], $color[1], $color[2]);
} // end func allocateColor
/**
* Returns the RGB integer values of an HTML like hexadecimal color like #00ff00.
*
* @param string HTML like hexadecimal color like #00ff00
* @return array [int red, int green, int blue],
* returns black [0, 0, 0] for invalid strings.
* @access public
*/
function HTMLColor2RGB($color) {
if (strlen($color) != 7)
return array(0, 0, 0);
return array(
hexdec(substr($color, 1, 2)),
hexdec(substr($color, 3, 2)),
hexdec(substr($color, 5, 2))
);
} // end func HTMLColor2RGB
/**
* Returns the RGB interger values of a named color, [0,0,0] if unknown.
*
* The class variable $colornames is used to resolve
* the color names. Modify it if neccessary.
*
* @param string Case insensitive color name.
* @return array [int red, int green, int blue],
* returns black [0, 0, 0] if the color is unknown.
* @access public
* @see $colornames
*/
function NamedColor2RGB($color) {
$color = strtolower($color);
if (!isset($this->colornames[$color]))
return array(0, 0, 0);
return $this->colornames[$color];
} // end func NamedColor2RGB
/**
* Returns the RGB integer values of a color specified by a "percentage string" like "%50,%20,%100".
*
* @param string
* @return array [int red, int green, int blue]
* @access public
*/
function PercentageColor2RGB($color) {
// split the string %50,%20,%100 by ,
$color = explode(",", $color);
foreach ($color as $k => $v) {
// remove the trailing percentage sign %
$v = (int)substr($v, 1);
// range checks
if ($v >= 100) {
$color[$k] = 255;
} else if ($v <= 0) {
$color[$k] = 0;
} else {
$color[$k] = (int)(2.55 * $v);
}
}
return $color;
} // end func PercentageColor2RGB
} // end class ColorHelper
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -