<p>I'm working on some code to colorize an image in Java. Basically what I'd like to do is something along the lines of GIMP's colorize command, so that if I have a BufferedImage and a Color, I can colorize the Image with the given color. Anyone got any ideas? My current best guess at doing something like this is to get the rgb value of each pixel in the BufferedImage and add the RGB value of the Color to it with some scaling factor.</p>

[{'answer_id': 23795, 'author': 'Nick', 'author_id': 1490, 'author_profile': 'https://Stackoverflow.com/users/1490', 'pm_score': 3, 'selected': False, 'text': '<p>Let <code>Y = 0.3*R + 0.59*G + 0.11*B</code> for each pixel in the image, then set them to be</p>\n\n<p><code>((R1+Y)/2,(G1+Y)/2,(B1+Y)/2)</code></p>\n\n<p>if <code>(R1,G1,B1)</code> is what you are colorizing with.</p>\n'}, {'answer_id': 27185, 'author': 'Bernie Perez', 'author_id': 1992, 'author_profile': 'https://Stackoverflow.com/users/1992', 'pm_score': 3, 'selected': True, 'text': '<p>I have never used GIMP\'s colorize command. However, if your getting the RGB value of each pixel and adding RGB value to it you should really use a <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/awt/image/LookupOp.html" rel="nofollow noreferrer">LookupOp</a>.</strong> Here is some code that I wrote to apply a BufferedImageOp to a BufferedImage.</p>\n\n<p>Using Nicks example from above heres how I would do it.</p>\n\n<blockquote>\n  <p>Let Y = 0.3*R + 0.59*G + 0.11*B for\n  each pixel</p>\n  \n  <p>(R1,G1,B1) is what you are colorizing\n  with</p>\n</blockquote>\n\n<pre><code>protected LookupOp createColorizeOp(short R1, short G1, short B1) {\n    short[] alpha = new short[256];\n    short[] red = new short[256];\n    short[] green = new short[256];\n    short[] blue = new short[256];\n\n    int Y = 0.3*R + 0.59*G + 0.11*B\n\n    for (short i = 0; i &lt; 256; i++) {\n        alpha[i] = i;\n        red[i] = (R1 + i*.3)/2;\n        green[i] = (G1 + i*.59)/2;\n        blue[i] = (B1 + i*.11)/2;\n    }\n\n    short[][] data = new short[][] {\n            red, green, blue, alpha\n    };\n\n    LookupTable lookupTable = new ShortLookupTable(0, data);\n    return new LookupOp(lookupTable, null);\n}\n</code></pre>\n\n<p>It creates a <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImageOp.html" rel="nofollow noreferrer">BufferedImageOp</a> that will mask out each color if the mask boolean is true.</p>\n\n<p>Its simple to call too.</p>\n\n<pre><code>BufferedImageOp colorizeFilter = createColorizeOp(R1, G1, B1);\nBufferedImage targetImage = colorizeFilter.filter(sourceImage, null);\n</code></pre>\n\n<p>If this is not what your looking for I suggest you look more into BufferedImageOp\'s.</p>\n\n<p>This is would also be more efficient since you would not need to do the calculations multiple times on different images. Or do the calculations over again on different BufferedImages as long as the R1,G1,B1 values don\'t change.</p>\n'}, {'answer_id': 4720882, 'author': 'nwodb.com', 'author_id': 579450, 'author_profile': 'https://Stackoverflow.com/users/579450', 'pm_score': 1, 'selected': False, 'text': "<p>I wanted to do the exact same thing as the question poster wanted to do but the above conversion did not remove colors like the GIMP does (ie green with a red overlay made an unpleasant brown color etc). So I downloaded the source code for GIMP and converted the c code over to Java.</p>\n\n<p>Posting it in this thread just in case anyone else wants to do the same (since it is the first thread that comes up in Google). The conversion still changes the white color when it should not, it's probably a casting issue from double to int. The class converts a BufferedImage in-place.</p>\n\n<pre><code>public class Colorize {\n\npublic static final int MAX_COLOR = 256;\n\npublic static final float LUMINANCE_RED   = 0.2126f;\npublic static final float LUMINANCE_GREEN = 0.7152f;\npublic static final float LUMINANCE_BLUE  = 0.0722f;\n\ndouble hue        = 180;\ndouble saturation =  50;\ndouble lightness  =   0;\n\nint [] lum_red_lookup;\nint [] lum_green_lookup;\nint [] lum_blue_lookup;\n\nint [] final_red_lookup;\nint [] final_green_lookup;\nint [] final_blue_lookup;\n\npublic Colorize( int red, int green, int blue )\n{\n   doInit();\n}\n\npublic Colorize( double t_hue, double t_sat, double t_bri )\n{\n   hue = t_hue;\n   saturation = t_sat;\n   lightness = t_bri;\n   doInit();\n}\n\npublic Colorize( double t_hue, double t_sat )\n{\n   hue = t_hue;\n   saturation = t_sat;\n   doInit();\n}\n\npublic Colorize( double t_hue )\n{\n   hue = t_hue;\n   doInit();\n}\n\npublic Colorize()\n{\n   doInit();\n}\n\nprivate void doInit()\n{\n   lum_red_lookup   = new int [MAX_COLOR];\n   lum_green_lookup = new int [MAX_COLOR];\n   lum_blue_lookup  = new int [MAX_COLOR];\n\n   double temp_hue = hue / 360f;\n   double temp_sat = saturation / 100f;\n\n   final_red_lookup   = new int [MAX_COLOR];\n   final_green_lookup = new int [MAX_COLOR];\n   final_blue_lookup  = new int [MAX_COLOR];\n\n   for( int i = 0; i &lt; MAX_COLOR; ++i )\n   {\n      lum_red_lookup  [i] = ( int )( i * LUMINANCE_RED );\n      lum_green_lookup[i] = ( int )( i * LUMINANCE_GREEN );\n      lum_blue_lookup [i] = ( int )( i * LUMINANCE_BLUE );\n\n      double temp_light = (double)i / 255f;\n\n      Color color = new Color( Color.HSBtoRGB( (float)temp_hue, \n                                               (float)temp_sat, \n                                               (float)temp_light ) );\n\n      final_red_lookup  [i] = ( int )( color.getRed() );\n      final_green_lookup[i] = ( int )( color.getGreen() );\n      final_blue_lookup [i] = ( int )( color.getBlue() );\n   }\n}\n\npublic void doColorize( BufferedImage image )\n{\n   int height = image.getHeight();\n   int width;\n\n   while( height-- != 0 )\n   {\n      width = image.getWidth();\n\n      while( width-- != 0 )\n      {\n         Color color = new Color( image.getRGB( width, height ) );\n\n         int lum = lum_red_lookup  [color.getRed  ()] +\n                   lum_green_lookup[color.getGreen()] +\n                   lum_blue_lookup [color.getBlue ()];\n\n         if( lightness &gt; 0 )\n         {\n            lum = (int)((double)lum * (100f - lightness) / 100f);\n            lum += 255f - (100f - lightness) * 255f / 100f;\n         }\n         else if( lightness &lt; 0 )\n         {\n            lum = (int)(((double)lum * lightness + 100f) / 100f);\n         }\n\n         Color final_color = new Color( final_red_lookup[lum],\n                                        final_green_lookup[lum],\n                                        final_blue_lookup[lum],\n                                        color.getAlpha() );\n\n         image.setRGB( width, height, final_color.getRGB() );\n\n      }\n   }\n}\n</code></pre>\n"}, {'answer_id': 20411373, 'author': '0circle0', 'author_id': 3072177, 'author_profile': 'https://Stackoverflow.com/users/3072177', 'pm_score': 2, 'selected': False, 'text': "<p>This works exactly like the Colorize function in GIMP and it preserves the transparency. I've also added a few things like Contrast and Brightness, Hue, Sat, and Luminosity - 0circle0 Google Me --> ' Sprite Creator 3'</p>\n\n<pre><code>import java.awt.Color;\nimport java.awt.image.BufferedImage;\n\npublic class Colorizer\n{\n    public static final int MAX_COLOR = 256;\n\n    public static final float LUMINANCE_RED = 0.2126f;\n    public static final float LUMINANCE_GREEN = 0.7152f;\n    public static final float LUMINANCE_BLUE = 0.0722f;\n\n    double hue = 180;\n    double saturation = 50;\n    double lightness = 0;\n\n    int[] lum_red_lookup;\n    int[] lum_green_lookup;\n    int[] lum_blue_lookup;\n\n    int[] final_red_lookup;\n    int[] final_green_lookup;\n    int[] final_blue_lookup;\n\n    public Colorizer()\n    {\n        doInit();\n    }\n\n    public void doHSB(double t_hue, double t_sat, double t_bri, BufferedImage image)\n    {\n        hue = t_hue;\n        saturation = t_sat;\n        lightness = t_bri;\n        doInit();\n        doColorize(image);\n    }\n\n    private void doInit()\n    {\n        lum_red_lookup = new int[MAX_COLOR];\n        lum_green_lookup = new int[MAX_COLOR];\n        lum_blue_lookup = new int[MAX_COLOR];\n\n        double temp_hue = hue / 360f;\n        double temp_sat = saturation / 100f;\n\n        final_red_lookup = new int[MAX_COLOR];\n        final_green_lookup = new int[MAX_COLOR];\n        final_blue_lookup = new int[MAX_COLOR];\n\n        for (int i = 0; i &lt; MAX_COLOR; ++i)\n        {\n            lum_red_lookup[i] = (int) (i * LUMINANCE_RED);\n            lum_green_lookup[i] = (int) (i * LUMINANCE_GREEN);\n            lum_blue_lookup[i] = (int) (i * LUMINANCE_BLUE);\n\n            double temp_light = (double) i / 255f;\n\n            Color color = new Color(Color.HSBtoRGB((float) temp_hue, (float) temp_sat, (float) temp_light));\n\n            final_red_lookup[i] = (int) (color.getRed());\n            final_green_lookup[i] = (int) (color.getGreen());\n            final_blue_lookup[i] = (int) (color.getBlue());\n        }\n    }\n\n    public void doColorize(BufferedImage image)\n    {\n        int height = image.getHeight();\n        int width;\n\n        while (height-- != 0)\n        {\n            width = image.getWidth();\n\n            while (width-- != 0)\n            {\n                Color color = new Color(image.getRGB(width, height), true);\n\n                int lum = lum_red_lookup[color.getRed()] + lum_green_lookup[color.getGreen()] + lum_blue_lookup[color.getBlue()];\n\n                if (lightness &gt; 0)\n                {\n                    lum = (int) ((double) lum * (100f - lightness) / 100f);\n                    lum += 255f - (100f - lightness) * 255f / 100f;\n                }\n                else if (lightness &lt; 0)\n                {\n                    lum = (int) (((double) lum * (lightness + 100f)) / 100f);\n                }\n                Color final_color = new Color(final_red_lookup[lum], final_green_lookup[lum], final_blue_lookup[lum], color.getAlpha());\n                image.setRGB(width, height, final_color.getRGB());\n            }\n        }\n    }\n\n    public BufferedImage changeContrast(BufferedImage inImage, float increasingFactor)\n    {\n        int w = inImage.getWidth();\n        int h = inImage.getHeight();\n\n        BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);\n        for (int i = 0; i &lt; w; i++)\n        {\n            for (int j = 0; j &lt; h; j++)\n            {\n                Color color = new Color(inImage.getRGB(i, j), true);\n                int r, g, b, a;\n                float fr, fg, fb;\n\n                r = color.getRed();\n                fr = (r - 128) * increasingFactor + 128;\n                r = (int) fr;\n                r = keep256(r);\n\n                g = color.getGreen();\n                fg = (g - 128) * increasingFactor + 128;\n                g = (int) fg;\n                g = keep256(g);\n\n                b = color.getBlue();\n                fb = (b - 128) * increasingFactor + 128;\n                b = (int) fb;\n                b = keep256(b);\n\n                a = color.getAlpha();\n\n                outImage.setRGB(i, j, new Color(r, g, b, a).getRGB());\n            }\n        }\n        return outImage;\n    }\n\n    public BufferedImage changeGreen(BufferedImage inImage, int increasingFactor)\n    {\n        int w = inImage.getWidth();\n        int h = inImage.getHeight();\n\n        BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);\n\n        for (int i = 0; i &lt; w; i++)\n        {\n            for (int j = 0; j &lt; h; j++)\n            {\n                Color color = new Color(inImage.getRGB(i, j), true);\n                int r, g, b, a;\n                r = color.getRed();\n                g = keep256(color.getGreen() + increasingFactor);\n                b = color.getBlue();\n                a = color.getAlpha();\n\n                outImage.setRGB(i, j, new Color(r, g, b, a).getRGB());\n            }\n        }\n        return outImage;\n    }\n\n    public BufferedImage changeBlue(BufferedImage inImage, int increasingFactor)\n    {\n        int w = inImage.getWidth();\n        int h = inImage.getHeight();\n\n        BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);\n\n        for (int i = 0; i &lt; w; i++)\n        {\n            for (int j = 0; j &lt; h; j++)\n            {\n                Color color = new Color(inImage.getRGB(i, j), true);\n                int r, g, b, a;\n                r = color.getRed();\n                g = color.getGreen();\n                b = keep256(color.getBlue() + increasingFactor);\n                a = color.getAlpha();\n\n                outImage.setRGB(i, j, new Color(r, g, b, a).getRGB());\n            }\n        }\n        return outImage;\n    }\n\n    public BufferedImage changeRed(BufferedImage inImage, int increasingFactor)\n    {\n        int w = inImage.getWidth();\n        int h = inImage.getHeight();\n\n        BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);\n\n        for (int i = 0; i &lt; w; i++)\n        {\n            for (int j = 0; j &lt; h; j++)\n            {\n                Color color = new Color(inImage.getRGB(i, j), true);\n                int r, g, b, a;\n                r = keep256(color.getRed() + increasingFactor);\n                g = color.getGreen();\n                b = color.getBlue();\n                a = color.getAlpha();\n\n                outImage.setRGB(i, j, new Color(r, g, b, a).getRGB());\n            }\n        }\n        return outImage;\n    }\n\n    public BufferedImage changeBrightness(BufferedImage inImage, int increasingFactor)\n    {\n        int w = inImage.getWidth();\n        int h = inImage.getHeight();\n\n        BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);\n\n        for (int i = 0; i &lt; w; i++)\n        {\n            for (int j = 0; j &lt; h; j++)\n            {\n                Color color = new Color(inImage.getRGB(i, j), true);\n\n                int r, g, b, a;\n\n                r = keep256(color.getRed() + increasingFactor);\n                g = keep256(color.getGreen() + increasingFactor);\n                b = keep256(color.getBlue() + increasingFactor);\n                a = color.getAlpha();\n\n                outImage.setRGB(i, j, new Color(r, g, b, a).getRGB());\n            }\n        }\n        return outImage;\n    }\n\n    public int keep256(int i)\n    {\n        if (i &lt;= 255 &amp;&amp; i &gt;= 0)\n            return i;\n        if (i &gt; 255)\n            return 255;\n        return 0;\n    }\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/23763', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/85/']