Imagemagick, Rmagick hsla problem

J

Justin Bl

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
 
T

Tim Hunter

Justin said:
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

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
 
J

Justin Bl

Tim said:
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')
 
T

Tim Hunter

Justin said:
Tim said:
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')

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.
 
J

Justin Bl

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

Tim said:
Justin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top