MKScripts for PixInsight

MK Color Range Selection

  • Version 1.1
  • Runs in PixInsight

Build a mask by clicking the colors you want to select, with live source and mask previews.

MK Color Range Selection creates a grayscale mask from colors you sample directly in a preview of your image. Each click adds a sample color; pixels close to it in RGB space are selected with a soft, distance-based falloff. Combine several samples, refine the result with tolerance, blur and opacity, and compare the source and mask side by side before creating the final mask image.

At a glance

  • Click-to-sample color picking in an interactive preview
  • Multiple selection mode that combines several sampled colors
  • Point, 3×3 and 5×5 averaged sampling for noisy data
  • Tolerance (sensitivity), mask blur and mask opacity controls
  • Mask inversion
  • Synchronized zoom and pan between the source and mask previews
  • Navigator mini-map and selectable cursor styles
  • Optional temporary auto-stretch for linear images
  • Undo of the last sampled color and one-click reset
Current version
1.1
Menu
Script › MK Scripts › Color Range Selection
Installed from
https://mhkastro.github.io/MKScripts/
Author
Mohammed Khalifa

The script window

The MK Color Range Selection window: instructions and mask controls on the left, the source nebula image in the middle and the generated grey-scale mask on the right. (opens a larger version)
MK Color Range Selection: the source image and the generated mask side by side.

Installation

Repository URL https://mhkastro.github.io/MKScripts/
  1. Open Manage Repositories. In PixInsight, choose Resources → Updates → Manage Repositories.
  2. Add the MKScripts repository. Click Add, paste the repository URL into the box and confirm.
  3. Check for updates. Choose Resources → Updates → Check for Updates and select the MKScripts packages you want.
  4. Exit PixInsight to install. The update is installed when you exit PixInsight, which then restarts automatically.

Full instructions: Installing MKScripts in PixInsight.

What the tool does#

MK Color Range Selection builds a grayscale mask from colors that you click in a preview of your image. Each sampled color selects the pixels whose color is close to it. Pixels that match exactly get full mask strength, and the strength fades linearly to zero as the color difference grows. You can combine several samples, then refine the mask with tolerance, blur and opacity before creating the final mask image.

The dialog shows the source image and the generated mask side by side, so you can see exactly what will be selected.

When to use it#

  • To isolate regions by color, for example emission nebulosity of one hue, so that a later process affects only those areas.
  • To prepare a mask for MK Colorize by Mask. The mask this script creates is named ColorMask, and Colorize by Mask automatically preselects an open image whose name contains "mask".
  • For grayscale images, as a brightness-range mask (see Tips).

Requirements and input preparation#

  • PixInsight 1.9.4 or a later 1.9.x version.
  • At least one open image. Otherwise the script stops with the message "Please open at least one image to use the Color Range Selection script."
  • Screen space. The dialog requests a minimum size of 1800 × 750 pixels and is resizable.
  • Color or grayscale. Color images use a distance in RGB space; grayscale images use the brightness difference.

Linear (unstretched) images#

Linear data usually looks almost black, which makes clicking the right colors difficult. Tick Linear Image (Auto Stretch) to apply a temporary automatic stretch.

Installation and launch#

  1. Install MKScripts from the repository: in PixInsight choose Resources → Updates → Manage Repositories → Add, paste https://mhkastro.github.io/MKScripts/, then run Resources → Updates → Check for Updates. Full steps: Installing MKScripts in PixInsight.
  2. Open the image you want to build a mask from.
  3. Choose Script › MK Scripts › Color Range Selection.

Basic workflow#

  1. Choose the target. Select the image in Target Image. The list shows every open image.
  2. Stretch if needed. For linear data, tick Linear Image (Auto Stretch).
  3. Set the sample size. For noisy data, choose Average (3x3) or Average (5x5).
  4. Sample a color. Left-click a point in the Source Image preview. The Generated Mask preview updates immediately.
  5. Add more colors (optional). Tick Multiple Selection (Additive), then click more points. Each sample adds to the mask. Undo Last removes the most recent sample.
  6. Refine. Adjust Tolerance, Mask Blur and Mask Opacity. Tick Invert Mask to swap selected and unselected areas.
  7. Inspect. Zoom with the mouse wheel and pan with the right or middle mouse button. Tick Sync Between Source and Mask Views to keep both previews aligned.
  8. Create the mask. Click Create Mask. A new image window named ColorMask opens. Use it as a mask in PixInsight in the usual way.
  9. Click Close when you are finished.

Controls#

Image and sampling#

Control Default What it does
Target Image First open image The image the mask is generated from. Changing it reloads the previews and clears samples.
Linear Image (Auto Stretch) Off Applies a temporary automatic stretch to the working copy. Sampling and mask calculation both use the stretched values.
Invert Mask Off Inverts the mask, so sampled colors become black and everything else white.
Multiple Selection (Additive) Off When on, each click adds a sample. When off, each click replaces all previous samples.
Sample Size Point (1x1) Point (1x1) reads a single pixel; Average (3x3) and Average (5x5) average the surrounding pixels. Pixels outside the image edge are ignored.
Cursor Style Arrow Arrow, Crosshair, Target Circle, Small Red Dot or Precision Reticle. Cosmetic only; applies to both previews.
Sampled Color — A swatch strip showing every sampled color, plus the R, G and B values (0–1) of the last sample and the total number of samples.
Undo Last — Removes the most recent sample and recalculates the mask.

Mask Controls#

Control Range Default What it does
Tolerance 1–100 20 How far a color may differ from a sample and still be selected. Higher values select a broader range of similar colors.
Mask Blur 0–100 0.0 Softens the mask with a Gaussian blur (PixInsight Convolution). The value is the blur's standard deviation in pixels. The slider moves in whole steps; the number box accepts decimals.
Mask Opacity 0–100 % 100 % Scales the whole mask. 0 % gives a black mask; 100 % keeps full strength.
Render Delay 0.0–1.0 s 0.4 s Waiting time after a slider change before the mask is recalculated. Higher values keep the dialog responsive on large images. Clicking a new sample always updates immediately.

Previews and navigation#

Action Result
Left-click in Source Image Samples a color. Clicks in the mask preview do not sample.
Mouse wheel Zooms in or out around the pointer, up to 800 %.
Right- or middle-button drag Pans the preview.
Double-click Zooms the preview to fit.
Sync Between Source and Mask Views Keeps zoom and position identical in both previews. Turning it off zooms the mask preview back to fit.
Navigator Click or drag in the mini-map to move the Source Image preview.

Buttons#

Button What it does
Create Mask Recalculates the mask with the current settings and opens it as a new image named ColorMask.
Close Closes the dialog and discards the temporary working images.
Reset Defaults Restores every control to its default value, resets the previews and clears all samples.

How the mask is calculated#

Understanding the calculation helps you choose settings:

  1. For each sample, the script measures each pixel's color difference from the sample: the straight-line distance between the two RGB colors for color images, or the absolute brightness difference for grayscale images.
  2. The mask value for that sample is 1 − distance ÷ (Tolerance ÷ 100 × 1.75), clipped at 0. An exact match gives 1; the value falls linearly to 0.
  3. With several samples, each pixel takes the highest value from any sample. Adding samples can only enlarge the selection.
  4. Invert Mask is applied next, then Mask Opacity, then Mask Blur.

Because opacity is applied after inversion, an inverted mask at 50 % opacity has a maximum value of 0.5.

Tips#

  • Noisy images: use Average (3x3) or Average (5x5) so that a single noisy pixel does not set the sample color.
  • Shades of one color: the color difference includes brightness, so a darker or brighter version of the same hue may fall outside the range. With Multiple Selection (Additive) on, sample the color at several brightness levels.
  • Grayscale images: the mask selects pixels by brightness, which makes the tool work like a luminance range mask.
  • Smooth transitions: a small amount of Mask Blur softens hard edges before the mask is used by another process.
  • Responsiveness: on small images, set Render Delay to 0.0 s for instant updates. On very large images, raise it.
  • Large blur values on large images take longer, because a full convolution runs each time the mask is recalculated.

Troubleshooting and limitations#

Symptom Explanation and solution
The script stops with "Please open at least one image…" Open an image first, then run the script again.
The mask is completely black No colors have been sampled yet. Left-click in the Source Image preview. (If Invert Mask is on, an empty mask is completely white.)
Clicking does not select anything Only left-clicks in the Source Image preview sample colors. The color is read where you release the left mouse button.
Left-dragging does not pan Pan with the right or middle mouse button.
My samples disappeared Changing the target image, toggling Linear Image (Auto Stretch) or clicking Reset Defaults clears all samples.
Sampled values look different from the original image With Linear Image (Auto Stretch) on, samples are taken from the stretched working copy.
The dialog does not fit on my screen The dialog requests a minimum size of 1800 × 750 pixels.
Sliders feel slow Increase Render Delay, or reduce Mask Blur while adjusting other settings.

Limitations:

  • The script works on the main view of an image, not on PixInsight previews.
  • The mask is a single-channel image with the same dimensions and sample format as the target.
  • While it runs, the script uses hidden temporary images whose names start with tmp_src_ and tmp_mask_. It closes them when the dialog closes.

Version information#

Item Value
Version 1.1
Source file MKColorRangeSelectionV1.1.js
Menu (feature ID) Script › MK Scripts › Color Range Selection
Installed from MKScripts repository (https://mhkastro.github.io/MKScripts/)
Repository release date 27 July 2026
PixInsight versions 1.9.4 – 1.9.x
Tested in PixInsight Not yet recorded for this guide

Release notes for each version are listed in the Releases section of this page.

Updates

New versions are delivered through the MKScripts repository: use Resources › Updates › Check for Updates in PixInsight.

License

Being publicly available does not make the scripts open source. See the terms of use.