开发者

How to convert html color names into RGB values in Ruby?

I know there is a gem called Color. I installed it.

But for the life of me, I can't figure out how to use the thing.

I just want to convert a color name into its RGB values, if possible without copying the whole colo开发者_JS百科r table into my code.

I want to be able to convert something like red or Navy into three numeric values.


require 'color/css'
red_code = Color::CSS["red"].html
#=> "#ff0000"


Old question, but I just came across this gem in a current project and had to do the same. I needed RBG values like OP asked for and so I used the css_rbg instance method similar to how tokland produced the hex value with the html instance method.

require 'color/css'
red_code = Color::CSS["red"].css_rgb
#=> "rgb(100.00%, 0.00%, 0.00%)"


Html colours are expressed as an Hexadecimal colour, and it turns out that a RGB colour is not more than a Hexadecimal expression of the colour so:

#ff0000 = r:255 g:0 b:0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜