Jump to content

CodeLab - First Steps (Color Sketch)


ReMake

Recommended Posts

Very simple effect which simulates a sketch by color pencils.

 

Open any image. I used a changed stock  photo from FireStock.

 

firestockwomanautumn.jpg

 

Create a copy of a background layer (or apply a combination of keys Ctrl+Shift+D).
Apply to this layer Invert Colors effect (Adjustments -> Invert Colors or apply a combination of keys Ctrl+Shift+I).

 

firestockwomanawkqz.jpg

 

Open Properties of a layer (key F4) and choose a blend mode Color Dodge.

 

firestockwomanaguuu.jpg

 

The active window will be filled with white color. Depending on the image selected by you, in some places there can be dark areas, but the majority of the document will be filled by the white.

 

Apply to the active layer Gaussian blur effect (Effects-> Blurs-> Gaussian Blur). Move a Radius control to the right before obtaining of necessary result. For this image I stopped on 3 pixels.

 

firestockwomanapcoa.jpg

 

Merge both layers (keys Ctrl+M) and we have necessary result.

Try to apply to this image Brightness/Contrast effect (Adjustments -> Brightness/Contrast), thus values of brightness and contrast should have identical values, but with an opposite sign (for example, at value of contrast equal 10 brightness should be equal -10).

 

firestockwomanazkrv.jpg

 

By the way, built-in Pencil Sketch effect is built on this same algorithm.

Now let's begin to create this effect.

 

Open CodeLab (Effects-> Advanced-> CodeLab). Start CodeLab (Effects-> Advanced-> CodeLab). Choose New in the menu File. In a dialogue box Script Editor click a No button. The dialogue box New Source (Template) will be presented to you. In the drop-down list choose Gaussian Blur. From the upper drop-down list PixelOp choose Invert and in the drop-down list Blend choose ColorDodge. Click the Generate Code button.

 

template.png

 

We will get the following script of effect:

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
IntSliderControl Amount1=10; // [0,100] Radius
#endregion

// Setup for using pixel op
private UnaryPixelOps.Invert invertOp = new UnaryPixelOps.Invert();

// Setup for using ColorDodge blend op
private BinaryPixelOp colordodgeOp = LayerBlendModeUtil.CreateCompositionOp(LayerBlendMode.ColorDodge);

// Here is the main render loop function
void Render(Surface dst, Surface src, Rectangle rect)
{
    // Setup for calling the Gaussian Blur effect
    GaussianBlurEffect blurEffect = new GaussianBlurEffect();
    PropertyCollection blurProps = blurEffect.CreatePropertyCollection();
    PropertyBasedEffectConfigToken BlurParameters = new PropertyBasedEffectConfigToken(blurProps);
    BlurParameters.SetPropertyValue(GaussianBlurEffect.PropertyNames.Radius, Amount1);
    blurEffect.SetRenderInfo(BlurParameters, new RenderArgs(dst), new RenderArgs(src));
    // Call the Gaussian Blur function
    blurEffect.Render(new Rectangle[1] {rect},0,1);
    // Now in the main render loop, the dst canvas has a blurred version of the src canvas
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra CurrentPixel = dst[x,y];

            // TODO: Add additional pixel processing code here
            CurrentPixel = invertOp.Apply(CurrentPixel);

            CurrentPixel = colordodgeOp.Apply(src[x,y], CurrentPixel);

            dst[x,y] = CurrentPixel;
        }
    }
}

Save this code with name ColorSketch (File -> Save or keys Ctrl+S).

Again choose New in the menu File. In the drop-down list choose Contrast. In the generated script select and copy (Ctrl+C) the following code:

    // Setup for calling the Brightness and Contrast Adjustment function
    BrightnessAndContrastAdjustment bacAdjustment = new BrightnessAndContrastAdjustment();
    PropertyCollection bacProps = bacAdjustment.CreatePropertyCollection();
    PropertyBasedEffectConfigToken bacParameters = new PropertyBasedEffectConfigToken(bacProps);
    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Brightness, Amount1);
    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Contrast, Amount2);
    bacAdjustment.SetRenderInfo(bacParameters, new RenderArgs(dst), new RenderArgs(src));
    // Call the Brightness and Contrast Adjustment function
    bacAdjustment.Render(new Rectangle[1] {rect},0,1);
    // Now in the main render loop, the dst canvas has an adjusted version of the src canvas

Load ColorSketch.cs script which was saved earlier (File -> Open or keys Ctrl+O) and paste this code before line

    for (int y = rect.Top; y < rect.Bottom; y++)

Open the designer interface (File -> User Interface Designer or keys Ctrl+I), select in it the Radius control and rename it to Pencil tip size. Set the maximum value to 20, set the default value to 2 and the minimum value to 1. Click the Update button. Select Integer Slider in the Control type box. Type Range in the Control name box. Set the maximum value to 20, set the default value to 0 and the minimum value to -20. Click the Add button. Click button OK.

In the line

    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Brightness, Amount1);

replace Amount1 on Amount2.

In the line

    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Contrast, Amount2);

set the minus sign before Amount2:

    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Contrast, -Amount2);

In the line

    bacAdjustment.SetRenderInfo(bacParameters, new RenderArgs(dst), new RenderArgs(src));

replace src on dst:

    bacAdjustment.SetRenderInfo(bacParameters, new RenderArgs(dst), new RenderArgs(dst));

Correct the comment:

    // Now in the main render loop, the dst canvas has an adjusted version of the src canvas
    // Now in the main render loop, the dst canvas has an adjusted version

Fill the commented lines at the top of the script, for example, like these:

// Name: Color Sketch
// Submenu: Artistic
// Author: ReMake
// Title: Color Sketch v1.0    ReMake 2016
// Version: 1.0
// Desc: The Paint.NET the effect which simulates a sketch by color pencils
// Keywords: paint.net|effect|color|sketch
// URL: http://www.getpaint.net/redirect/plugins.html
// Help:

Our final script should look like this:

// Name: Color Sketch
// Submenu: Artistic
// Author: ReMake
// Title: Color Sketch v1.0    ReMake 2016
// Desc: The Paint.NET the effect which simulates a sketch by color pencils
// Keywords: paint.net|effect|color|sketch
// URL: http://www.getpaint.net/redirect/plugins.html
#region UICode
int Amount1=2; // [1,20] Pencil tip size
int Amount2=0; // [-20,20] Range
#endregion

// Setup for using pixel op
private UnaryPixelOps.Invert invertOp = new UnaryPixelOps.Invert();

// Setup for using ColorDodge blend op
private UserBlendOps.ColorDodgeBlendOp colordodgeOp = new UserBlendOps.ColorDodgeBlendOp();

// Here is the main render loop function
void Render(Surface dst, Surface src, Rectangle rect)
{
    // Setup for calling the Gaussian Blur effect
    GaussianBlurEffect blurEffect = new GaussianBlurEffect();
    PropertyCollection blurProps = blurEffect.CreatePropertyCollection();
    PropertyBasedEffectConfigToken BlurParameters = new PropertyBasedEffectConfigToken(blurProps);
    BlurParameters.SetPropertyValue(GaussianBlurEffect.PropertyNames.Radius, Amount1);
    blurEffect.SetRenderInfo(BlurParameters, new RenderArgs(dst), new RenderArgs(src));
    // Call the Gaussian Blur function
    blurEffect.Render(new Rectangle[1] {rect},0,1);
    // Now in the main render loop, the dst canvas has a blurred version of the src canvas

    // Setup for calling the Brightness and Contrast Adjustment function
    BrightnessAndContrastAdjustment bacAdjustment = new BrightnessAndContrastAdjustment();
    PropertyCollection bacProps = bacAdjustment.CreatePropertyCollection();
    PropertyBasedEffectConfigToken bacParameters = new PropertyBasedEffectConfigToken(bacProps);
    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Brightness, Amount2);
    bacParameters.SetPropertyValue(BrightnessAndContrastAdjustment.PropertyNames.Contrast, -Amount2);
    bacAdjustment.SetRenderInfo(bacParameters, new RenderArgs(dst), new RenderArgs(dst));
    // Call the Brightness and Contrast Adjustment function
    bacAdjustment.Render(new Rectangle[1] {rect},0,1);
    // Now in the main render loop, the dst canvas has an adjusted version

    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra CurrentPixel = dst[x,y];

            // TODO: Add additional pixel processing code here
            CurrentPixel = invertOp.Apply(CurrentPixel);

            CurrentPixel = colordodgeOp.Apply(src[x,y], CurrentPixel);

            dst[x,y] = CurrentPixel;
        }
    }
}

Save it with the same name (ColorSketch.cs).

Create for this effect an icon in the PNG format with the size 16x16 pixels. I used the modified icon of the built-in Pencil Sketch effect: colorsketch.png
Save this effect as a DLL file (File -> Save as DLL or keys Ctrl+В). Try this effect in Paint.NET.

The user interface of our new effect looks like this.

 

colorsketchui.png

I hope this has not caused difficulties for you.

 

Edited by ReMake
  • Like 2
  • Upvote 1
Link to comment
Share on other sites

  • 2 years later...

I wanted to thank you, also, @ReMake,  for the informative series of coding lessons.

 

Kudos for posting all this information to us 'coding newbies'!  ?

  • Upvote 1
Link to comment
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.

×
×
  • Create New...