在PHP中将RGB转换为十六进制颜色值 [英] Convert RGB to hex color values in PHP
本文介绍了在PHP中将RGB转换为十六进制颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我有
$color = rgb(255, 255, 255);
我想将其转换为十六进制颜色代码.输出类似
i want to convert this into hex color code.Out put like
$color = '#ffffff';
推荐答案
一个简单的 sprintf
即可.
A simple sprintf
will do.
$color = sprintf("#%02x%02x%02x", 13, 0, 255); // #0d00ff
要分解格式:
-
#
-文字字符# -
%
-转换规范开始 -
0
-用于填充的字符 -
2
-转换应导致的最少字符数,并在必要时用以上字符填充 -
x
-该参数被视为整数,并以十六进制数字表示,并带有小写字母 -
%02x%02x
-以上四个重复两次
#
- the literal character #%
- start of conversion specification0
- character to be used for padding2
- minimum number of characters the conversion should result in, padded with the above as necessaryx
- the argument is treated as an integer and presented as a hexadecimal number with lowercase letters%02x%02x
- the above four repeated twice more
这篇关于在PHP中将RGB转换为十六进制颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文