Create multi color transparent gradient with sparse-color

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
haarts

Create multi color transparent gradient with sparse-color

Post by haarts »

I want to create a gradient with more than two colors and one of the colors should be transparent. Searching the forum I found that -sparse-color in combination with the Shepard method could do the trick. So I tried:

Code: Select all

convert -size 50x500 xc: -channel RGBA -sparse-color  Shepards '0,0 rgb(0,255,0,1) 0,250 rgb(255,255,255,0) 0,500 rgb(255,0,0,1)' test.png
The resulting test.png doesn't include the alpha channel. I don't quite understand why. The docs state that one should define -channel and the colors are correct as well as far as I can tell.
haarts

Re: Create multi color transparent gradient with sparse-colo

Post by haarts »

On a related note, I'm trying to understand the -fx operator. I have:

Code: Select all

-fx "v.p{0,u*v.h}"
This means that of the second image (v) the pixelvalue (p{}) is used for the resulting image. But I'm unclear on what pixelvalue is used. The first column obviously, but what row? u is the first image, v the second and h the height? If the first image is greyscale and the second a gradient image as described in my previous post what is the result? I assumed this was a color replacement. A mapping of greyscale to color space. But in that light the multiplier does not make sense to me.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create multi color transparent gradient with sparse-colo

Post by fmw42 »

haarts wrote:I want to create a gradient with more than two colors and one of the colors should be transparent. Searching the forum I found that -sparse-color in combination with the Shepard method could do the trick. So I tried:

Code: Select all

convert -size 50x500 xc: -channel RGBA -sparse-color  Shepards '0,0 rgb(0,255,0,1) 0,250 rgb(255,255,255,0) 0,500 rgb(255,0,0,1)' test.png
The resulting test.png doesn't include the alpha channel. I don't quite understand why. The docs state that one should define -channel and the colors are correct as well as far as I can tell.
try adding -alpha set or -matte after -channel RGBA
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Create multi color transparent gradient with sparse-colo

Post by anthony »

haarts wrote:On a related note, I'm trying to understand the -fx operator. I have:

Code: Select all

-fx "v.p{0,u*v.h}"
This means that of the second image (v) the pixelvalue (p{}) is used for the resulting image. But I'm unclear on what pixelvalue is used. The first column obviously, but what row? u is the first image, v the second and h the height? If the first image is greyscale and the second a gradient image as described in my previous post what is the result? I assumed this was a color replacement. A mapping of greyscale to color space. But in that light the multiplier does not make sense to me.

v.p{x,y} will look up the pixel at the point x,y in the second image 'v'
The x,y coordinate may be a floating point position so interpolation may be used to mixx colors of near by pixels.

So 0 means just the first 'zeroth' column of pixels will be used,
u*v.h mean the y coodinate is the color of the current pixel in the first image (in 0 to 1 range) multiplied by the height of thesecond image.

So this FX expression replaces the original with a lookup of its color in the second image.
that is replace pixel colors using a Color Lookup Table (the -clut operator is essentuallty does same thing (with minor improvements)

See IM Examples, Color LUT
http://www.imagemagick.org/Usage/color/#clut
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
haarts

Re: Create multi color transparent gradient with sparse-colo

Post by haarts »

anthony wrote:
v.p{x,y} will look up the pixel at the point x,y in the second image 'v'
The x,y coordinate may be a floating point position so interpolation may be used to mixx colors of near by pixels.

So 0 means just the first 'zeroth' column of pixels will be used,
u*v.h mean the y coodinate is the color of the current pixel in the first image (in 0 to 1 range) multiplied by the height of thesecond image.

So this FX expression replaces the original with a lookup of its color in the second image.
that is replace pixel colors using a Color Lookup Table (the -clut operator is essentuallty does same thing (with minor improvements)

See IM Examples, Color LUT
http://www.imagemagick.org/Usage/color/#clut
Now I see! Thanks for clearing this up for me. And I replaced the -fx expression with the -clut expression. Many thanks for the tip.
haarts

Re: Create multi color transparent gradient with sparse-colo

Post by haarts »

fmw42 wrote: try adding -alpha set or -matte after -channel RGBA
Adding -alpha resulted in an error and -matte didn't do anything as far as I can see. I resolved the issue by -append and using different gradients. Thanks for the help though!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Create multi color transparent gradient with sparse-colo

Post by anthony »

that should have been "-alpha set"

also to specify transparency in a color you need rgba() nor rgb()

Code: Select all

 convert -size 50x500 xc: -alpha set -channel RGBA \
             -sparse-color  Shepards '0,0     rgba(0,255,0,1) 
                                                0,250  rgba(255,255,255,0) 
                                                0,500  rgba(255,0,0,1)'       test.png
WARNING: shepards currently does not properly handle alpha (sorry)
As such the above will shade the colors from the opaque colro toward transparent white.

Replace the rgba(255,255,255,0) with 'none' to see it shade toward black which makes the problem very visible!

I have added this problem onto my ToDo list.

It is also mishandling -channel A. for example this replaces all existing colors with black!

Code: Select all

    convert logo: -alpha set -channel A -sparse-color Barycentric \
                 '0,0  opaque     0,%h transparent' \
            show:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply