Rails show rgb color string as color swatch

旧时模样 提交于 2019-12-11 11:03:16

问题


Im using RMagick to take the average color of an image a user uploads. and I've got it down to displaying as RGB format. Now I'd like to take that RGB string and display it as a color swatch on the page...any idea on how to accomplish this?

{:r=>155, :g=>132, :b=>118}

回答1:


# controller
@image = Magick::Image.read(@design.photo.path).first
average_color = # your magick method to return the average color
# so average is like " {:r=>155, :g=>132, :b=>118} "
@average_color_string = "##{average_color.values.map{|v| v.to_s(16) }.join}"

#view
Average color: <%= @average_color_string %>

should display something like this:

Average color: #9b8476

The .to_s(16) converts the Integer into a 16-based string, also known as Hexadecimal.



来源:https://stackoverflow.com/questions/19227763/rails-show-rgb-color-string-as-color-swatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!