# Imagemagick, Rmagick hsla problem

**URL:** <https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455>\
**Category:** Ruby\
**Created:** [August 9, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455 "2009-08-09T17:35:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [August 9, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455/1 "2009-08-09T17:35:20Z")

</div>

For any imagemagick users out there, I’m having trouble getting  
Rmagick’s hsl functions to work for me.

I use the following code:  
@canvas.background\_color = Magick::Pixel.from\_hsla(80, 100, 50, 1)

To try and set a HSLA background color for my image, but it keeps coming  
out white.  
Any idea why? The image is made okay, no errors are thrown in the ruby  
code.

Ruby version: 1.8.6  
ImageMagick Version: 6.5.3 Q8  
Rmagick Version: rmagick-2.10.0-x86-mswin32  
OS: Windows Vista

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [August 9, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455/2 "2009-08-09T17:50:40Z")

</div>

Justin Bl wrote:

> Ruby version: 1.8.6  
> ImageMagick Version: 6.5.3 Q8  
> Rmagick Version: rmagick-2.10.0-x86-mswin32  
> OS: Windows Vista

I don’t think the problem is with the from\_hsla method. This produces a  
nice sorta lime-green 200x200 image for me. Why don’t you post a  
complete script that reproduces the problem?

irb(main):001:0\> p = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
=\> red=170, green=255, blue=0, opacity=0  
irb(main):002:0\> img = Magick::Image.new(200,200) {  
self.background\_color = p}  
=\> 200x200 DirectClass 8-bit  
irb(main):003:0\> img.display  
=\> 200x200 DirectClass 8-bit

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [August 9, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455/3 "2009-08-09T18:12:14Z")

</div>

Tim H. wrote:

> Justin Bl wrote:
> 
> > Ruby version: 1.8.6  
> > ImageMagick Version: 6.5.3 Q8  
> > Rmagick Version: rmagick-2.10.0-x86-mswin32  
> > OS: Windows Vista
> 
> I don’t think the problem is with the from\_hsla method. This produces a  
> nice sorta lime-green 200x200 image for me. Why don’t you post a  
> complete script that reproduces the problem?
> 
> irb(main):001:0\> p = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
> =\> red=170, green=255, blue=0, opacity=0  
> irb(main):002:0\> img = Magick::Image.new(200,200) {  
> self.background\_color = p}  
> =\> 200x200 DirectClass 8-bit  
> irb(main):003:0\> img.display  
> =\> 200x200 DirectClass 8-bit

Tim,  
Full source code is below:

require ‘rubygems’  
require ‘rvg/rvg’  
include Magick

class MyImage  
def initialize (job\_id, width, height)

@job\_id = job\_id.to\_s;  
@width = width.to\_i;  
@height = height.to\_i;  
@canvas = Magick::Image.new(@width, @height)  
#@canvas.colorspace = HSLColorspace  
@canvas.background\_color = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
#it will make the pixel but it will not make it the right color  
end  
def draw ()

# choose random color and put it in a pixel

#@canvas.each\_pixel {|pixel, c, r|  
#ind = rand(5)  
#pixel = $colors[ind]  
#}  
#@canvas.pixel\_color(x, y[, new\_color]) -\> pixel

end  
def finalize (type=“gif”)  
@canvas.write("#{@job\_id}.#{type}")  
end  
end

x = 500  
y = 500  
increm = 360/5

$colors = Array.new  
$colors = [  
[0,100,50,1],  
[72,100,50,1],  
[144,100,50,1],  
[216,100,50,1],  
[288,100,50,1]  
]

# initial array is just the h s l a values

$colors = $colors.map { |xx| Magick::Pixel.from\_hsla(xx[0], xx[1],  
xx[2], x[3]) }

# convert it to array of pixels

puts $colors[0].class

img = MyImage.new(“hsl\_test”, x, y)  
img.draw()  
img.finalize(‘png’)

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [August 9, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455/4 "2009-08-09T18:19:56Z")

</div>

Justin Bl wrote:

> > irb(main):001:0\> p = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
> > require ‘rubygems’  
> > #@canvas.colorspace = HSLColorspace
> 
> $colors = Array.new
> 
> # convert it to array of pixels
> 
> puts $colors[0].class
> 
> img = MyImage.new(“hsl\_test”, x, y)  
> img.draw()  
> img.finalize(‘png’)

The color of a new image is set when the image is created. Do this:

@canvas = Magick::Image.new(@width, @height) do  
self.background\_color = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
end

Setting the background\_color attribute _after_ creating the image only  
changes the value of the background\_color attribute. It doesn’t actually  
change the color of the image.

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [August 9, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/imagemagick-rmagick-hsla-problem/173455/5 "2009-08-09T18:40:11Z")

</div>

Tim,  
Thanks very much for your help.  
The code you posted works.

Tim H. wrote:

> Justin Bl wrote:
> 
> > > irb(main):001:0\> p = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
> > > require ‘rubygems’  
> > > #@canvas.colorspace = HSLColorspace
> > 
> > $colors = Array.new
> > 
> > # convert it to array of pixels
> > 
> > puts $colors[0].class
> > 
> > img = MyImage.new(“hsl\_test”, x, y)  
> > img.draw()  
> > img.finalize(‘png’)
> 
> The color of a new image is set when the image is created. Do this:
> 
> @canvas = Magick::Image.new(@width, @height) do  
> self.background\_color = Magick::Pixel.from\_hsla(80, 100, 50, 1)  
> end
> 
> Setting the background\_color attribute _after_ creating the image only  
> changes the value of the background\_color attribute. It doesn’t actually  
> change the color of the image.
