Popular Post MJW 1,328 Posted June 7, 2016 Popular Post Share Posted June 7, 2016 The Color Clearer plugin makes pixels that match the selected color transparent, and modifies the transparency and color of the other pixels to maximize their transparency while still producing the original color when blended with the normal blending mode to a background set to the selected color. It some sense, it removes as much of the selected color as possible from each pixel. Here is the (still simple) user interface: The Make Transparent Pixels Transparent White check box specifies that transparent pixels will be set to transparent white, rather than have their colors remain unchanged. I was careful to try to make the blended image after Color Clearer exactly match the original blended image, even when the original pixels are partially transparent. I hope I succeeded. Interestingly (I think), all the math is done using integer arithmetic. I attempted to do the same thing in HSV Eraser, but I didn't take into account the precise way PDN alpha-blends layers, so there were many cases where the resultant blended pixels were off by one. Here's an example, using an image from a previous discussion. The original image is: After using the Color Picker to choose the red background color, then running Color Clearer, the result is: If the second image is placed on top of a layer filled with the background color, the merged layers match the original image. Here is the DLL: ColorClearer.zip Maximilian has a 3.5.11 version. Here is the CodeLab code: Spoiler // Name: Color Clearer // Submenu: Color // Author: MJW // Title: Color Clearer // Version: 1.1.* // Desc: Make a selected color transparent. // Keywords: color transparent alpha // URL: // Help: #region UICode ColorWheelControl Amount1 = ColorBgra.FromBgr(0, 0, 0); // Color to Make Transparent CheckboxControl Amount2 = false; // [0,1] Make Transparent Pixels Transparent White #endregion ColorBgra transpWhite = ColorBgra.FromBgra(255, 255, 255, 0); void Render(Surface dst, Surface src, Rectangle rect) { bool makeTranspWhite = Amount2; int Rb = Amount1.R, Gb = Amount1.G, Bb = Amount1.B; // Save some later multiplications. Might or might not be more efficient. int sRb = 255 * Rb, sGb = 255 * Gb, sBb = 255 * Bb; for (int y = rect.Top; y < rect.Bottom; y++) { for (int x = rect.Left; x < rect.Right; x++) { ColorBgra Cf = src[x, y]; int Af = Cf.A; if (Af == 0) { // Optionaly set the transparent pixel to transparent white. if (makeTranspWhite) Cf = transpWhite; } else { int Rf = Cf.R, Gf = Cf.G, Bf = Cf.B; // Alpha-blend the foreground and background colors using the same method // as PDN. The test for alpha == 255 is for efficiency, since it's // probably a very common case. int Rc, Gc, Bc; if (Af == 255) { // No blending required. Rc = Rf; Gc = Gf; Bc = Bf; } else { Rc = (sRb + Af * (Rf - Rb)) / 255; Gc = (sGb + Af * (Gf - Gb)) / 255; Bc = (sBb + Af * (Bf - Bb)) / 255; } // Compute the minimum alpha that will allow the blended pixel color to // remain unchanged. The minimum alpha for each individual component // is computed, and the maximum of of those alphas is use. int Ac = MinimumAlpha(Rc, Rb); Ac = Math.Max(Ac, MinimumAlpha(Gc, Gb)); Ac = Math.Max(Ac, MinimumAlpha(Bc, Bb)); if (Ac == 0) { // A non-transparent pixel was made transparent (so the forground color // equals the background color). The color is currently the original // value. Optionally set it to transparent white. Cf = makeTranspWhite ? transpWhite : Cf.NewAlpha(0); } else { // Compute the new color components for the mimimized alpha. byte r = AdjustForAlpha(Ac, Rc, Rb, Rf); byte g = AdjustForAlpha(Ac, Gc, Gb, Gf); byte b = AdjustForAlpha(Ac, Bc, Bb, Bf); Cf = ColorBgra.FromBgra(b, g, r, (byte)Ac); } } dst[x, y] = Cf; } } } // Find the lowest alpha that will allow this component of the color to remain unchanged // if blended against the background color. int MinimumAlpha(int Cc, int Cb) { if (Cc == Cb) return 0; else if (Cc > Cb) return (255 * (Cc - Cb) - 1) / (255 - Cb) + 1; else return 255 * (Cb - Cc - 1) / Cb + 1; } // Adjust the color component for the new alpha so that it remains unchanged if blended // against the background color. Cm is the color component to match as nearly as possible // (likely the original foreground color). byte AdjustForAlpha(int Af, int Cc, int Cb, int Cm) { int T = 255 * (Cc - Cb) - Af * (Cm - Cb); if (T <= -255) Cm += (T + 255) / Af - 1; else if (T > 0) Cm += (T - 1) / Af + 1; return (byte)Cm; } EDIT: As noted next to the DLL, I intend to modify the plugin to choose better replacement colors. The current values work, in that the produce the proper colors when blended with the background color, but they are sometimes somewhat illogical choices. EDIT 2: A new version. I changed the color used when a number of colors will produce the correct blended result. I now choose the color that's nearest to the original color. For consistency, I left completely transparent pixels unchanged (except for alpha), unless an option is selected to make them transparent white. 11 Quote Link to post Share on other sites
homebrew 0 Posted June 7, 2016 Share Posted June 7, 2016 what is the difference between color clearer and reverse blend set to average color; i've tried and got the same things with the picture of spider. on the other hand on the bomber i've applied manual color correction and then magic wand with first tolerance to 0 to get fine outline and 16 to achieve a good result; better than with anything else. the two pictures got non uniform blue color backgrounds. Quote Link to post Share on other sites
lynxster4 3,220 Posted June 7, 2016 Share Posted June 7, 2016 (edited) Hi MJW! I've tried your new plugin with the example pic I used on 'Make Transparent' plugin the other day. Here are the results: Original Make Transparent Color Clearer I notice that your plugin seems to be more precise. It left some of a really dark shade of orange (more red I believe) behind. Is this correct? Thanks! Edited July 27, 2017 by lynxster4 re-hosted image 1 1 Quote My Art Gallery | My Shape Packs | ShapeMaker Mini Tut | Air Bubble Stained Glass Chrome Text with Reflections | Porcelain Text w/ Variegated Coloring | Realistic Knit PatternOpalescent Stained Glass | Frosted Snowman Cookie | Leather Texture | Plastic Text | Silk Embroidery Visit my Personal Website "Never, ever lose your sense of humor - you'll live longer" Link to post Share on other sites
MJW 1,328 Posted June 7, 2016 Author Share Posted June 7, 2016 homebrew: Obviously Color Clearer is much simpler than Reverse Blend. It was intended to resemble Make Transparent. Besides simplicity -- which is an advantage if that's exactly what you want to do -- the main advantage over Reverse Blend is that Color Clearer handles partial transparency correctly. Color Clearer is also slightly more accurate than Reverse Blend. I endeavored to make it exactly match the original image when blended to a background of the selected color. So far, it looks like I succeeded, but there are lots of combinations of color and transparency, and I certainly haven't tested them all. I'll mention that handling partial transparency turns out to be remarkably easy. I thought it would be something like multiplying the computed alpha by the image pixel alpha. Instead, it's simply a matter of computing the new alpha and color relative to the image color alpha-blended with the selected color. That's the color that has to be matched. lynxster4: I'd always assumed Make Transparent reduced the alpha and modified the color so that the image didn't change when blended to the selected color. I intended to modify it to handle transparency. I discovered, though, that when the image color is light gray, and the selected color is dark gray, blending to the dark gray does not produce the original color. (I believe Make Transparent only works the way I'd expected when all the color components are 0 or 255.) It may be a bug, or it may be that Make Transparent simply wasn't intended to do what I expected it to do. In any case, because it required a completely different algorithm and produced substantially different results, I decided to write my own plugin instead of modifying Make Transparent. So in summary, if you want to remove the background color in a way that it will match the original image when blended with the background color, Make Transparent won't do it while Color Clearer will. (Reverse Blend will also work, provided the image doesn't have partially transparent pixels.) Quote Link to post Share on other sites
MJW 1,328 Posted June 8, 2016 Author Share Posted June 8, 2016 lynxster4: It left some of a really dark shade of orange (more red I believe) behind. Is this correct? Of course there's always the possibility of a bug, but I doubt that's the case here. Reverse Blend behaves the same way. The reason is probably that the orange contains a lot of green, while the red color contains very little. In order to blend to the original color, the alpha must be fairly large, since a small value results in too much green being blended in. One of the color components of the modified color is always either 0 or 255. Otherwise, alpha could be decreased. In this case green is 0, making the color redder than originally. 1 Quote Link to post Share on other sites
MJW 1,328 Posted June 8, 2016 Author Share Posted June 8, 2016 I just realized there's an oddity, which isn't exactly a bug, but nevertheless bugs me. When calculating the new color values, there is often a range of values for each of the RGB components that will result in the same color, especially for small alphas. Currently, I rather arbitrarily use the highest value. The result of this is that if for one of the RGB color components, the selected color value is the same as the color value being matched, the computed value might be another, different value. That doesn't seem to make much sense, so I think I'll fix it. I'm not sure of the best way, though. The easy way is to test to see if the color values match, and if so, skip the normal computation and use that value. I may, though, want to change the computation to use the middle of the range, or something like that, in a way that gives the input values when they match. I'll need to think about it. Quote Link to post Share on other sites
Maximilian 1,927 Posted June 10, 2016 Share Posted June 10, 2016 (edited) I've been able to modify “dull” white clouds and blue skies into something more interesting by combining Hot Metal Glow with Color Clearer —at least within the scope of what I consider an interesting sky. Here's an example in which I use Color Clearer to remove the red “bloody” areas created by Hot Metal Glow while preserving the rest intact: The Hot Metal Glow settings I apply to a duplicate of the sky layer are: Celsius Temperature = 0, Orange Desaturation = 1, Invert Color Range = True, and everything else at defaults. Next, I apply Color Clearer to this hot-metal-glowed layer while keeping the original sky layer below it, in two steps, first selecting #C00000 as the color to make transparent, and subsequently choosing pure red as the color to make transparent. Finally, I add a black and white gradient layer set to overlay above both sky layers (this is to fade some of the clouds and to add a more vivid light to the finished sky). Everything was tested on a 100% PdN sky layer, but I guess it may work well on a photo of a real sky (check pending). I've also tried to mimic the color clearing steps with the Make Transparent plugin, but it threw me an error at times and the aspect was not quite similar whenever I got it to work. Besides, I noticed that using the plugin for a second time on an already red-free sky layer caused the transparent areas to become filled with black, whereas the remaining areas turned into jagged yellowish stains. Thanks a lot one more time, MJW! You always come up with the right effect I need for special compositions. I'm super glad you're this good at implementing such nifty algorithms Color Clearer 1.1 for PdN 3.5.11.zip Edited September 30, 2018 by Maximilian 1 2 Quote Link to post Share on other sites
MJW 1,328 Posted June 10, 2016 Author Share Posted June 10, 2016 Thank you for the 3.5.11 build, Maximilian. I feel a little guilty, but I'm going to have an updated version of Color Clearer soon. I figured out a reasonably good solution to the problem I mentioned in my previous comment. It took me a while to figure out the math. 1 Quote Link to post Share on other sites
Maximilian 1,927 Posted June 10, 2016 Share Posted June 10, 2016 No problem. As soon as you share the updated code, I can try to update my compilation as well. I hope the new code will compile correctly for me. Otherwise, I'll have to start asking questions if I run into difficulties 1 Quote Link to post Share on other sites
MJW 1,328 Posted June 10, 2016 Author Share Posted June 10, 2016 There should be no problem compiling it. There's nothing fancy about the changes. Quote Link to post Share on other sites
Maximilian 1,927 Posted June 10, 2016 Share Posted June 10, 2016 Then it will be done! 1 Quote Link to post Share on other sites
MJW 1,328 Posted June 15, 2016 Author Share Posted June 15, 2016 I posted a new version which I believe makes a better choice for the adjusted color. I also added an additional control that allows a choice for transparent pixels between the original color or transparent white. 1 1 Quote Link to post Share on other sites
Maximilian 1,927 Posted June 15, 2016 Share Posted June 15, 2016 I've also recompiled and updated my post above with version 1.1 for PdN 3.5.11 Thanks once again, MJW! 1 Quote Link to post Share on other sites
Seerose 4,276 Posted June 23, 2016 Share Posted June 23, 2016 MJW! Thank you very much for the plugin. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi Link to post Share on other sites
BlackRedDead 0 Posted October 6, 2019 Share Posted October 6, 2019 (edited) Thx for the Headsup, i saw that your Entry "Maximilian has a 3.5.11 version." just leads back to this thread, maybe you missed to link to a specific post? (if so check that "Share this Post" Icon in the Upper-Right of the specific Post 😉 Edit: and for a next version you could group your Plugin under "Transparency" to reduce Clutter of the menu 😉 (instead creating your own "Color" group xP) Edited October 6, 2019 by BlackRedDead Quote Link to post Share on other sites
MJW 1,328 Posted October 6, 2019 Author Share Posted October 6, 2019 Thanks. I fixed the link. The old version of the link used to work, but apparently there was some change to the forum that made it not work. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.