如何将字符串颜色转换为其十六进制代码或RGB值? [英] How to convert a string color to its hex code or RGB value?

查看:412
本文介绍了如何将字符串颜色转换为其十六进制代码或RGB值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否可能,但我想创建一些函数,你传递一个字符串,如黄色或绿色,并返回其十六进制代码或十六进制代码的范围,绿色或黄色适合。

I am not sure if its possible but I would like to create some sort of function where you pass in a string such as "yellow" or "green" and it returns its hex code or the range of hex codes that greens or yellows fit into.

这在PHP可能吗?

感谢所有

推荐答案

我知道无法转换 HTML颜色名称 CSS颜色名称转换为PHP中的Hex或RGB值。你必须创建一个知道名字的地图(见Wiki文章)。

There is no way to my knowledge to convert HTML color names or CSS color names to their Hex or RGB value in PHP. You'd have to create a map that knows the names (see Wiki article).

有人已经为你完成了这项工作:

Someone has done this work for you already:

  • http://psoug.org/snippet/CSS_Colornames_to_RGB_values_415.htm

摘录:

$Colors  =  ARRAY( 
    "black"=>array( "red"=>0x00,  "green"=>0x00,  "blue"=>0x00), 
    "maroon"=>array( "red"=>0x80,  "green"=>0x00,  "blue"=>0x00), 
    // ...
    // more colors inbetween
    // ...
    "wheat"=>array( "red"=>0xF5,  "green"=>0xDE,  "blue"=>0xB3), 
    "whitesmoke"=>array( "red"=>0xF5,  "green"=>0xF5,  "blue"=>0xF5), 
    "yellowgreen"=>array( "red"=>0x9A,  "green"=>0xCD,  "blue"=>0x32)
); 

,然后通过 $ maroon = $ Colors [ maroon'];

进一步参考:

  • http://www.w3.org/TR/css3-color/#html4
  • http://www.crockford.com/wrrrld/color.html

这篇关于如何将字符串颜色转换为其十六进制代码或RGB值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆