A Script Request (RWPaint user) - RealWorld forums

Log-in or register.

A Script Request (RWPaint user)

plus an idea for forum expansion
smyskr
on September 2nd 2013

Hi, I've been using RWPaint for all my art projets for the past 2-3 years. It was so perfect for my needs that I didn't need to ask for anything.

Because I didn't know ( :-( ) it's scripting capabilities.

Now I need something: Can you whip up a script to replace a particular exact color with another?

also, it might be a good and useful idea to create a forum section for hosting nifty scripts many users have surely come up with.

Vlasta
on September 2nd 2013

If you go to "Effect"->"Custom operation..." and paste this into the Configurqation tab:

Configuration.AddColorButton("src", "Original", "", 0);
Configuration.AddColorButton("dst", "Replace by", "", 0xffffff);

...and this into the Execution tab:

var img = Document.RasterImage;
var src = Configuration.src;
var dst = Configuration.dst;
var sizeX = img.sizeX;
var sizeY = img.sizeY;

for (var x=0; x<sizeX; ++x)
{
	for (var y=0; y<sizeY; ++y)
	{
		if (img.GetPixelAlpha(x, y, 0, 0) != 0 &&
			img.GetPixelColor(x, y, 0, 0) == src)
			img.SetPixelColor(x, y, 0, 0, dst);
	}
}

it should do what you want.

If you have the 2013.1 version, you can also download this and drag and drop it onto the toolbar with "Adjust exposure", "Drop shadow" and other effects and it will add a new button with the above pre-configured.

smyskr
on September 2nd 2013

Perfect!!! You just made my day!!!
This does exactly what I needed.

now, what about the scripts forum I talked about?

Vlasta
on September 2nd 2013

Well, if people start to contribute scripts or talk about them, I'll make the forum section. But at this moment, it would be almost empty. There is also another possibility - registered users may create or modify pages in the Help section, so that is an alternative. The Javascript operation page may be a good candidate, where to place links to scripts.

smyskr
on September 3rd 2013

I'll learn and try to contribute.

Meanwhile, there seems to be a problem with the color replacing script. if I execute the script in a selection, why does the whole selected region sometimes go up a few pixels when clicked outside the selection?

Vlasta
on September 3rd 2013

You are talking about the floating selection, right? (The script or the way it is plugged into the application would have to be modified if it should work with the non-floating selections.)

I did a few tests and did not notice the region moving up. I noticed the region being trimmed if there is empty space selected, but the valid pixels remained in place. Could you find a situation, in which the pixels move after clicking outside and describe it here so I can replicate it?

smyskr
on September 3rd 2013

Sorry for the delay, I was out on an errand.

Anyways, as I've said the problem happens irregularly. but it seems to occur if I do the replace operation 3-4 times. Here's a video (~300KB): https://www.dropbox.com/s/ovw62feg3azjqwo/20130903_153843.asf.
So perhaps you can create a new file and randomly execute the script a few times on different areas with different colors.
I removed my previous AppData folder to ensure it's due to no customization.

Vlasta
on September 3rd 2013

Great, I'll see what can be done about it. It looks like an error.

In the meantime, here is an alternative script that also works with image mask. So you can select a region with one of the tools in the first group (to the left of Transformation) and apply the effect on that region.

smyskr
on September 4th 2013

Alright. Thanks again.

Vlasta
on September 5th 2013

A (hopefully working) fix is available. After downloading RWDrawing.dll, overwrite the old file and it should not move the selection anymore.

smyskr
on September 5th 2013

Great! This works flawlessly.
I'll keep using the replacement script, which works with both types of selection, Remove the first script?

Ugotsta
on September 18th 2013

This is such an awesome feature of RWPaint, to be able to create effects on-the-fly using JavaScript.

Here's something quick I threw together for a customizable scanline effect. Horizontal, vertical or both scanlines are possible with the scanline_x and scanline_y variables and the alpha value of the effect can be adjusted with scanline_alpha.

var img = Document.RasterImage;
var sizeX = img.sizeX;
var sizeY = img.sizeY;
var scanline_x = 2;
var scanline_y = 0;
var scanline_alpha = 0;
for (var x = 0; x < sizeX; x += 1) {
  for (var y = 0; y < sizeY; y += 1) {
		if (x%scanline_x === 0) {
			img.SetPixelAlpha(x, y, 0, 0, scanline_alpha);
		}
		if (y%scanline_y === 0) {
			img.SetPixelAlpha(x, y, 0, 0, scanline_alpha);
		}
	}
}

I also posted this up as a Gist:
https://gist.github.com/Ugotsta/6613836

Again, awesome feature. Thanks Vlasta for an incredible editor!

Ugotsta
on September 18th 2013

Hmm, how do we properly format code here?

Vlasta
on September 18th 2013

space in front of every line will make it look like code - I just did it

Anonymous
on November 4th 2013

+1

I love it, works perfectly!

Now to work on my spritesheet recolor project!

PAEz
on November 4th 2013

Hey Anonymous,
If your thinking of doing a sprite recolour thingy have you thought of doing like a colour mask style thing.
Instead of changing one colour to another (which can get real tricky if youve got shades) have another layer that has a mask for the colours.
So you could have 4 colours that can be changed, you draw a mask for each color over the sprite with a different color for each area.
Then you could write a script that has a configuration panel allowing you to pick a colour for each colour mask.
Then instead of changing one colour to another you just change the hue (and maybe saturation, but just hue is the safe one) of these pixels. This way the pixels keep their shade.
Prob is theres no hue/saturation picker for the configuration so it might not have the expected behaviour for a user, but youll know whats going on if its just for you.
If you wanted to change the lightness aswell you could always scan the whole mask, find the highest lightness value and map a gradient according to that. So that the lightness of the selected color is the maximum lightness for that colour mask.
Anyways, just thinking out loud ;-)

PAEz
on November 4th 2013

Actually you can do this now without scripting.
On top of the sprites layer create a separate layer for each colour mask.
Then set each of these colour masks blend mode to Replace hue.
Now what ever pixels are under them will change colour but not lightness or saturation.
Then you can just use Adjustments - more adjustments - Shift Hue, to change the colour of each mask layer.

smyskr
on June 12th 2015

Another variation that replaces all invalid transparent pixels (reason)

Set up a new command like this:

Command  : Document Operation
Name     : [0409]Replace Invalid Pixels
Operation: JavaScript
  Operation: Raster Image - Mask
  Execution: // Paste the following js in the Execution textbox
// replaces non-black 100%-transparent
// pixels in selection / document with
// 100%-transparent black pixels.
var img   = Document.RasterImage;
var invisiblack = 0x00000000;
var sizeX = img.sizeX, sizeY = img.sizeY;
for (var x=0; x<sizeX; ++x) {
  for (var y=0; y<sizeY; ++y) {
    if (img.GetPixelAlpha(x, y, 0, 0) == 0)
      img.SetPixel(x, y, 0, 0, invisiblack);
  }
}
Nibbler
on September 27th 2016

It's not working on RealWorld Cursor Editor. It says img.sizeX is null or not an object. I want it to be used on RealWorld Cursor Editor.

Page views: 2655       Posts: 19      
I wish there were...
Select background
What about ICL files?