From 7d3439ad7612ec4f6b63c28ffd3a77a377712d5b Mon Sep 17 00:00:00 2001 From: gongfuxiang <2499232802@qq.com> Date: Sat, 8 Jun 2019 18:41:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=A2=9C=E8=89=B2=E5=80=BC?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common.php | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/application/common.php b/application/common.php index c452fae7a..f330d2480 100755 --- a/application/common.php +++ b/application/common.php @@ -11,6 +11,72 @@ // 应用公共文件 +/** + * RGB 转 十六进制 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-06-08T18:38:16+0800 + * @param [string] $rgb [reg颜色值] + */ +function RgbToHex($rgb) +{ + $regexp = "/^rgb\(([0-9]{0,3})\,\s*([0-9]{0,3})\,\s*([0-9]{0,3})\)/"; + preg_match($regexp, $rgb, $match); + $re = array_shift($match); + $hex_color = "#"; + $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); + for ($i = 0; $i < 3; $i++) + { + $r = null; + $c = $match[$i]; + $hex_array = []; + while ($c > 16) + { + $r = $c % 16; + $c = ($c / 16) >> 0; + array_push($hex_array, $hex[$r]); + } + array_push($hex_array, $hex[$c]); + $ret = array_reverse($hex_array); + $item = implode('', $ret); + $item = str_pad($item, 2, '0', STR_PAD_LEFT); + $hex_color .= $item; + } + return $hex_color; +} + +/** + * 十六进制 转 RGB + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-06-08T18:33:45+0800 + * @param [string] $hex_color [十六进制颜色值] + */ +function HexToRgb($hex_color) { + $color = str_replace('#', '', $hex_color); + if(strlen($color) > 3) + { + $rgb = [ + 'r' => hexdec(substr($color, 0, 2)), + 'g' => hexdec(substr($color, 2, 2)), + 'b' => hexdec(substr($color, 4, 2)) + ]; + } else { + $color = $hex_color; + $r = substr($color, 0, 1) . substr($color, 0, 1); + $g = substr($color, 1, 1) . substr($color, 1, 1); + $b = substr($color, 2, 1) . substr($color, 2, 1); + $rgb = [ + 'r' => hexdec($r), + 'g' => hexdec($g), + 'b' => hexdec($b) + ]; + } + return $rgb; +} + /** * 字符串转ascii * @author Devil -- GitLab