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: BlackAndWhite+.zip

 

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.

Normalize Weights: When enabled, the Red, Green, and Blue Weights will be divided by their sum so that the total weight is 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:
// 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
CheckboxControl Amount4 = true; // [0,1] Normalize Weights
DoubleSliderControl Amount5 = 1.0; // [0,4] Brightness
DoubleSliderControl Amount6 = 0.0; // [-1,1] Midpoint Adjustment
CheckboxControl Amount7 = false; // [0,1] Use Primary Color and Secondary Color
CheckboxControl Amount8 = false; // [0,1] Show Original Image
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    double rW = Amount1, gW = Amount2, bW = Amount3;
    double intensScale = Amount5 / 255.0;
    if (Amount4)
    {
        double norm = rW + gW + bW;
        if (norm != 0.0)
            intensScale /= norm; 
    }
    rW *= intensScale; gW *= intensScale; bW *= intensScale;
    double adjScale = -Amount6;
    double adjOffset = Amount6 + 1.0;  
    
    ColorBgra bColor, wColor;
    if (Amount7)
    {
        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 (Amount8)
            {
                dst[x, y] = pix;
            }
            else
            {
                double intensity = rW * pix.R + gW * pix.G + bW * pix.B;
                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.

  • 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

I know how to do it in VS, but for simplicity, this was written using CodeLab.

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
  • Upvote 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now