问题描述
我一直在CSS中使用3位十六进制颜色值: #fff
,#999
,#069
等。我可以看到重复的字母/数字如何合并创建一个3位的十六进制颜色代码,但我不完全理解模式能够在PHP中编写转换器。有这样的文档吗?
I've been using 3-digit hex color values in CSS for a long time: #fff
, #999
, #069
, etc. I can see how the repeating letters/numbers are merged to create a 3-digit hex color code, but I don't fully understand the pattern to be able to write a converter in PHP. Is there documentation for this?
编辑:哦,也许我的问题不清楚。我需要知道一些6位十六进制颜色值如何转换为3位数。 xxxxxx
( ffffff
)和 xxyyzz
(<$ c $
Oh, perhaps my question wasn't clear. I need to know how some of the 6-digit hex color values are converted to 3-digits. xxxxxx
(ffffff
) and xxyyzz
(006699
) – these are the only two patterns, correct?
推荐答案
要转换3 -character十六进制代码转换为6个字符的一个,需要重复每个字符:
To convert a 3-character hex code into a 6 character one, you need to repeat each character:
$hex = '#fff';
$hex6 = '#' . $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
如果要将其转换为十进制,可以使用函数
If you want to convert it to decimal you can use the hexdec
function
这篇关于转换为3位十六进制颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!