سؤال

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

should display something like this:

Average color: #9b8476

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top