Rails显示rgb颜色字符串作为颜色样本 [英] Rails show rgb color string as color swatch

查看:219
本文介绍了Rails显示rgb颜色字符串作为颜色样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用RMagick拍摄用户上传的图片的平均颜色。我已经得到它显示为RGB格式。现在我想拿这个RGB字符串并将其显示为页面上的颜色样本...有关如何完成此任务的任何想法?

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}


推荐答案

# 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 %>

应显示如下:


平均颜色:#9b8476

Average color: #9b8476

.to_s c $ c>将整数转换为基于16的字符串,也称为十六进制

这篇关于Rails显示rgb颜色字符串作为颜色样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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