如何将16进制颜色值直接转换为RGB色值(hex2rgb)?
- 内容介绍
- 文章标签
- 相关推荐
本文共计156个文字,预计阅读时间需要1分钟。
python/*** 16进制颜色转换成RGB颜色值* @method hex2rgb* @param {string} $hexColor - 16进制颜色代码* @return {string} RGB颜色值*/function hex2rgb($hexColor) { $color=str_replace('#', '', $hexColor); // 确保颜色代码长度为6 if (strlen($color) !=6) { return false; } // 分解颜色代码 $r=hexdec(substr($color, 0, 2)); $g=hexdec(substr($color, 2, 2)); $b=hexdec(substr($color, 4, 2)); // 返回RGB颜色值 return sprintf(#%02x%02x%02x, $r, $g, $b);}
/** * 16进制颜色转换为RGB色值 * @method hex2rgb */ function hex2rgb($hexColor) { $color = str_replace('#', '', $hexColor); if (strlen($color) > 3) { $rgb = array( '0' => hexdec(substr($color, 0, 2)), '1' => hexdec(substr($color, 2, 2)), '2' => hexdec(substr($color, 4, 2)) ); } else { $color = str_replace('#', '', $hexColor); $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 = array( '0' => hexdec($r), '1' => hexdec($g), '2' => hexdec($b) ); } return $rgb; }
本文共计156个文字,预计阅读时间需要1分钟。
python/*** 16进制颜色转换成RGB颜色值* @method hex2rgb* @param {string} $hexColor - 16进制颜色代码* @return {string} RGB颜色值*/function hex2rgb($hexColor) { $color=str_replace('#', '', $hexColor); // 确保颜色代码长度为6 if (strlen($color) !=6) { return false; } // 分解颜色代码 $r=hexdec(substr($color, 0, 2)); $g=hexdec(substr($color, 2, 2)); $b=hexdec(substr($color, 4, 2)); // 返回RGB颜色值 return sprintf(#%02x%02x%02x, $r, $g, $b);}
/** * 16进制颜色转换为RGB色值 * @method hex2rgb */ function hex2rgb($hexColor) { $color = str_replace('#', '', $hexColor); if (strlen($color) > 3) { $rgb = array( '0' => hexdec(substr($color, 0, 2)), '1' => hexdec(substr($color, 2, 2)), '2' => hexdec(substr($color, 4, 2)) ); } else { $color = str_replace('#', '', $hexColor); $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 = array( '0' => hexdec($r), '1' => hexdec($g), '2' => hexdec($b) ); } return $rgb; }

