Search code examples
rubyimagemagickrmagick

Create angle gradient with RMagick


I want to create an angle gradient with RMagick. The final result should look something like the image below. I must be able to define the positions of the colors.

enter image description here

According to this page of the ImageMagick documentation it is possible with the below command.

convert -size 50x1000 gradient: -rotate 90 -alpha set \
          -virtual-pixel Transparent +distort Polar 49 +repage \
          -transverse  gradient_angle_masked.png

However, I am completely new to RMagick and by reading its documentation I have not been able to figure out how to translate their command to commands in RMagick.

Can anyone help me translate the command or point me in the right direction? Any sample code would be greatly appreciated.


Solution

  • Refer to:

    RMagick: Magick Command Options and Their Equivalent Methods

    You can find the corresponding RMagick methods/attributes of the command line arguments. The command you gave can be translated to the following Ruby code. However, to get the image you provided, you'll have to dig more.

    require 'RMagick'
    
    fill = Magick::GradientFill.new(0, 0, 0, 1000, '#FFF', '#000')
    image = Magick::Image.new(50, 1000, fill)
    image.rotate(90)
    image.alpha(Magick::SetAlphaChannel)
    image.virtual_pixel_method = Magick::TransparentVirtualPixelMethod
    image = image.distort(Magick::PolarDistortion, [0]) do
      self.define('distort:Radius_Max', 49)
    end
    image.transpose
    image = image.crop(0, 475, 50, 50, true)
    image.write('test.png')
    

    Some pages you might be interested:

    Hope that helps.