This project has moved. For the latest updates, please go here.

Issue with colorization using RGB values

Jul 14, 2016 at 2:39 PM
I have an image I'd like to colorize using RGB values (and eventually, HSL), but for whatever reason I'm only able to pass in a hex value as a parameter for my image to be successfully colorized.

Is there anything inherently different between:

image.Colorize(new MagickColor(255, 255, 0), (Percentage)50); -DOES NOT WORK
image.Colorize(new MagickColor("#ffff00"), (Percentage)50); -WORKS

Assistance with this would be greatly appreciated, thanks.

Jul 14, 2016 at 7:12 PM
I suspect that you are using the Q16 version of Magick.NET. The MagickColor constructor that accepts ushort as an argument has a range of 0-65535 and not 0-255 as you are probably used to from System.Drawing. I will make sure that the next version of Magick.NET has better xml documentation that explains the valid range.
Jul 14, 2016 at 9:18 PM
Edited Jul 14, 2016 at 9:21 PM
Great! That works for me.

One more question for you...

I was wondering how I would go about tinting an image with a specified color while preserving the white values. Currently, I'm able to tint a black to white gradient in yellow using:

image.Colorize(new MagickColor("#ffff00"), (Percentage)50);

This is the result:

However, what I would really like to achieve is something this (not exact shade of yellow I used):

Any idea on how I would go about doing that? This is assuming that I feed in a picture of a simple black to white gradient, so essentially I just need the black and shades of grey to translate to the various shades of whatever color I select.

Thanks again!
Jul 15, 2016 at 7:23 AM
Edited Jul 15, 2016 at 7:37 AM
Can you add a link to an input image and show the output? Having trouble to understand your goal. You might be able to turn the gray parts into black and use that as a mask for when you colorize the image.
Jul 15, 2016 at 2:39 PM
Hi Dirk,

Thank you for your quick response. My apologies for not being clearer...

Here's another original image:

What I'm trying to achieve:

This is what I get currently...
...using this:
image.Colorize(new MagickColor("#ffff00"), (Percentage)50);

Instead of washing out the black/white hues, I'd like to only saturate the various shades of grey with shades of whatever hex color I pass in.

If you load up Photoshop, go to Image > Hue/Saturation and configure the HSL for 60/70%/50%, you'll get the desired result.

How would I go about doing this, given these values?

Jul 18, 2016 at 1:35 PM
Any ideas?
Jul 18, 2016 at 7:21 PM
I could not figure out how to do that. You can ask for help here: I can help you translate it to Magick.NET if you manage to figure out how to do it on the command line.
Jul 18, 2016 at 11:14 PM
Hi Dirk,

Thanks for pointing me in the right direction. I was told to try this:

convert background1_original.jpg \
( -size 1x1 xc:black xc:"hsl(60,70%,50%)" xc:white +append -resize 512x1! ) \
-interpolate bilinear -clut result.jpg

How would I go about translating that?
Jul 21, 2016 at 7:12 PM
Edited Jul 21, 2016 at 7:12 PM
I can translate this command but I don't get the correct result due to a bug in the OpenCL resize code. This will be resolved this weekend and then I will post both translated examples from the ImageMagick post here:
Jul 21, 2016 at 7:33 PM
Great - thank you for your kind attention. I'll be checking in this weekend :)
Jul 23, 2016 at 1:57 PM
The new release has been published. Below is a translation of the two commands that you got on the ImageMagick forum:
// convert background1_original.jpg
using (var image = new MagickImage("background1_original.jpg"))
  using (var images = new MagickImageCollection())
    // ( -size 1x1 xc:black xc:"hsl(60,70%,50%)" xc:white
    images.Add(new MagickImage("xc:black", 1, 1));
    images.Add(new MagickImage("xc:hsl(60%,70%,50%)", 1, 1));
    images.Add(new MagickImage("xc:white", 1, 1));

    // +append
    using (var clut = images.AppendHorizontally())
      // -resize 512x1! )
      clut.Resize(new MagickGeometry("512x1!"));
      // -interpolate bilinear -clut
      image.Clut(clut, PixelInterpolateMethod.Bilinear);
      // result.jpg

// magick background1_original.jpg -append ) -clut output.jpg
using (var image = new MagickImage("background1_original.jpg"))
  using (var images = new MagickImageCollection())
    // (-size 1x256 gradient:#000000-#dddd33 gradient:#dddd33-#ffffff
    images.Add(new MagickImage("gradient:#000000-#dddd33", 1, 256));
    images.Add(new MagickImage("gradient:#dddd33-#ffffff", 1, 256));

    // -append )
    using (var clut = images.AppendVertically())
      // -clut
      image.Clut(clut, PixelInterpolateMethod.Undefined);
      // result.jpg
Jul 25, 2016 at 8:50 PM
Hi Dirk,

The first solution works beautifully and is exactly what I was looking for. Thank you for your hard work and generosity.