MJW

Black and White+

Recommended Posts

Black and White+ is a simple plugin to convert an image to black and white. The primary additional option (that may or may not merit the "+") is the ability to specify the weights for each color channel. It's in the Adjustment menu.

 

The plugin: 

 

The Help Menu description:

 

Black and White+ converts a color image to a grayscale (or two-color) image.

The controls are:

Red Weight: The weight for the red channel when converting from color to brightness.

Green Weight: The weight for the green channel when converting from color to brightness.

Blue Weight: The weight for the blue channel when converting from color to brightness.

Minimum-Channel Weight: The weight for the minimum channel when converting from color to brightness.

Maximum-Channel Weight: The weight for the maximum channel when converting from color to brightness.

Normalize Weights: When enabled, the weights will be divided by their sum so that the total weight is one. (Note: If only a single channel is non-zero, normalizing the weights will cause it to always have a weight of one.)

Brightness: Increases or decreases the brightness.

MIdpoint Adjustment: Increases or decreases the brightness of the center values while not affecting the lowest and highest values.

Use Primary Color and Secondary Color: When enabled, the brightness will be used to interpolate between the Primary Color and the Secondary Color instead of black and white.

Show Original Image: When enabled, the unmodified image will be displayed.

 

The CodeLab code:

Spoiler

// Name: Black and White+
// Submenu:
// Author: MJW
// Title: Black and White+
// Version: 1.1.*
// Desc: Convert color to black and white, with adjustable weights.
// Keywords: color black white
// URL: https://forums.getpaint.net/topic/110935-black-and-white/
// Help:
#region UICode
DoubleSliderControl Amount1 = 0.299; // [0,1] Red Weight
DoubleSliderControl Amount2 = 0.587; // [0,1] Green Weight
DoubleSliderControl Amount3 = 0.114; // [0,1] Blue Weight
DoubleSliderControl Amount4 = 0.0; // [0,1] Minimum-Channel Weight
DoubleSliderControl Amount5 = 0.0; // [0,1] Maximum-Channel Weight
CheckboxControl Amount6 = true; // [0,1] Normalize Weights
DoubleSliderControl Amount7 = 1.0; // [0,4] Brightness
DoubleSliderControl Amount8 = 0.0; // [-1,1] Midpoint Adjustment
CheckboxControl Amount9 = false; // [0,1] Use Primary Color and Secondary Color
CheckboxControl Amount10 = false; // [0,1] Show Original Image
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    double rW = Amount1, gW = Amount2, bW = Amount3;
    double minW = Amount4, maxW = Amount5;
    bool useMinMax = ((minW != 0.0) || (maxW != 0.0));
    double intensScale = Amount7 / 255.0;
    if (Amount6)
    {
        double norm = rW + gW + bW + minW + maxW;
        if (norm != 0.0)
            intensScale /= norm; 
    }
    rW *= intensScale; gW *= intensScale; bW *= intensScale;
    minW *= intensScale; maxW *= intensScale;
    
    double adjScale = -Amount8;
    double adjOffset = Amount8 + 1.0;  
    
    ColorBgra bColor, wColor;
    if (Amount9)
    {
        bColor =  (ColorBgra)EnvironmentParameters.PrimaryColor;
        wColor =  (ColorBgra)EnvironmentParameters.SecondaryColor;
    }
    else
    {
        bColor = ColorBgra.Black;
        wColor = ColorBgra.White;        
    }
    
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra pix = src[x, y];
            if (Amount10)
            {
                dst[x, y] = pix;
            }
            else
            {
                double intensity = rW * pix.R + gW * pix.G + bW * pix.B;
                if (useMinMax)
                {
                    int min = Math.Min(pix.R, Math.Min(pix.G, pix.B));
                    int max = Math.Max(pix.R, Math.Max(pix.G, pix.B));
                    intensity += minW * min + maxW * max;                  
                }
                if (intensity > 1.0)
                    intensity = 1.0;
                
                // Apply the midpoint adjustment to the clamped intensity.
                intensity *= adjScale * intensity + adjOffset;
                
                dst[x, y] = ColorBgra.Lerp(bColor, wColor, intensity).NewAlpha(pix.A);
            }
        }
    }
}

 

 

The icon: BlackAndWhite+.png

 

Maximilian has a 3.5.11 version.

 

EDIT (6/17/2018): Version 1.1. Added controls for min and max color channels.

EDIT (6/18/2018): Version 1.1.6744.2631. Corrected website URL.

BlackAndWhite+.zip

  • Upvote 8

Share this post


Link to post
Share on other sites

You're welcome, Eli. I'll do my best to officially publish the various plugins I posted to other threads.

Share this post


Link to post
Share on other sites

In my opinion if Red Weight, Green Weight, and Blue Weight controls have three digits after the decimal point, it's logical to assume that the sampling step should be 0.001 instead of 0.01.

Share this post


Link to post
Share on other sites

ReMake, I agree with you, but for CodeLab plugins, it can't be changed. You'll have to talk to BoltBait about that. I have ideas for a few additional features for this plugin. If I add them, I'll make this a VS plugin.

Share this post


Link to post
Share on other sites

Yes, I meant compilation of a plug-in in the VS:

configUI.SetPropertyControlValue(PropertyNames.Amount1, ControlInfoPropertyNames.UpDownIncrement, 0.001);

 

Share this post


Link to post
Share on other sites

Many thanks for this, MJW! I can always make use of a b/w converter.

 

I've compiled a version for PdN 3.5.11 and I'm aware that CodeLab 1.8 has an issue setting default values with decimal points, but I'm dropping this comment here in case I'm missing something and someone has a workaround that would allow me to set the default weights to values other than 1 (I chose 1 because setting the weights to 0 would render an all black image... surely I'm saying the too obvious).

 

Also, is there any icon I could use?

Share this post


Link to post
Share on other sites

Maximilian, I added the icon at the bottom of the opening comment.

 

I remember the problem with CodeLab (sometimes?) thinking non-integer defaults meant it was an angle control, but I don't remember if there was a work-around. I thought there were versions of CodeLab that worked with older versions of PDN that didn't have that bug.

Share this post


Link to post
Share on other sites

Thanks again, MJW. I think there's no other option but setting the weights to 1 for users of the old PdN, since 1.8 seems to be the only old version of CodeLab that can still be found and it does have said bug. But it's a small issue, all things considered.

 

Edit: I've recompiled and reuploaded my compilation of this plugin so that the default values for users of PdN 3.5.11 are also the original default values in the source code, thanks to the fact that MJW kindly provided me with a better version of CodeLab for my old PC (and also thanks to my discovering how to set ranges for sliders in my old system :P). Thanks once more, MJW!

 

Black and White+ for PdN 3.5.11.zip

 

Edited by Maximilian
  • Like 1
  • Upvote 1

Share this post


Link to post
Share on other sites

I'm releasing version 1.1, which has additional controls to use the minimum and maximum channels.

 

NOTE: HSV Value is the max channel; HSL Lightness is the average of the min and max channels.

  • Like 1
  • Upvote 1

Share this post


Link to post
Share on other sites

This issue has come up before.

 

Naming conventions require something a little more unique than other plugins. PDN seems to read only the first entry and misrepresents the second. Not a BIG deal - but annoying to a perfectionist. ?

 

Black_And_White_Plus2.png

Share this post


Link to post
Share on other sites

I fixed a problem with the support info. The website URL was bad, which for some reasion makes the other support info not show up.

 

(I'd fixed the URL, but apparently forgot to save the corrected source file, so it remained bad after the next edi

 

@AndrewDavid  I don't see it appearing twice in the list you show. I only see the built-in Black and White, followed by Black and White+. Maybe I misunderstand your point.

Share this post


Link to post
Share on other sites

Yes you did misunderstand.

Look at the incorrect version number it displays when hovering the cursor over the plugin menu entry while inside Paint. It is not really 0.0.0.0  

When looking at the DLL through explorer it shows the correct version number 1.1.6742.32785 as of your last compiled DLL

Share this post


Link to post
Share on other sites

I see. That's the problem I think I fixed after you posted yesterday. I noticed the invalid support info from your post, but that doesn't seem to match what IRON67 was writing about. Does the updated version show up correctly? As I mentioned, I think that problem resulted from having an bad URL string.

Share this post


Link to post
Share on other sites

Thanks Andrew. Glad to hear it's fixed. I'm annoyed with myself for not getting it fixed in the original release. As I mentioned, I fixed it in the source file, then forgot to save the change.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.