0

我如何通过 PHP 从颜色字符串中获取单独的通道,例如:“#1122f0”来获得这个:

$color = substr("#1122f0", 1); // get "1122f0"
// wanted:
$red = 0x11;
$green = 0x22;
$blue = 0xf0;
4

1 回答 1

1
list($red, $green, $blue) = array_map('dechex',
  sscanf('#1122f0', '#%02x%02x%02x'));

您还可以将组件解析为字符串:

list($red, $green, $blue) = sscanf('#1122f0', '#%02s%02s%02s');

但是第一个解决方案也会(某种程度上)验证您的颜色字​​符串:)

于 2013-02-16T23:52:05.900 回答