Marquee to Gradient for Affinity
dailyRushes Scripts for Affinity
Marquee to Gradient reads the colours under a marquee and turns them into an editable gradient, with stops and midpoints placed to match the image. You can lighten, darken, recolour and simplify it, then add it as a gradient rectangle, a banded colour map, or both.
Making a gradient
- Select the layer and draw the marquee.
- In the Script Library, click Marquee to Gradient. The window opens with two gradient strips.
- Adjust the gradient with the buttons and swatches.
- Choose what to make under Create.
- Click OK to add it, or Cancel to stop without adding anything.
If there is no marquee when you run the script, a window says Select a marquee area first, with these steps. Nothing else happens.
The two strips
- Sampled gradient, at the top: the gradient as read from the image, with its number of stops. It never changes, so you can compare against it.
- Adjusted gradient, below it: the gradient you are working on. This is the one used for the rectangle when you click OK.
Which way it reads
A marquee wider than it is tall is read left to right. One taller than it is wide is read top to bottom. Across the marquee’s thickness the colours are averaged, so a thicker marquee smooths out texture and grain.
How the stops are placed
The script uses as few stops as it can while staying within a few colour levels of the image. Each stop also gets a midpoint, set where the blend to the next stop really reaches halfway, so uneven blends are matched without extra stops. To change how many stops there are, see Adjusting.
Good to know
- A marquee that runs past the layer is trimmed to the layer. Only the part over the layer’s visible pixels is read, so the gradient has no black ends, and the rectangle is made the width of that part.
- Transparent areas are ignored. Holes inside the layer, such as a gap in a painted shape, are filled by blending between the colours either side.
- A 1-pixel marquee can be repeated across the whole layer, to make full-length stripes. See Output.
- Placed images that have been moved or scaled are read correctly, at the image’s own pixels.
- The Scripting panel shows the area that was read, for example “Sampled: x 129–509, y 259–271”, and says when the marquee was trimmed.
- Your document is not changed until you click OK. Previews on the canvas are removed when the window closes, and Cancel adds nothing.
If something looks wrong
| What you see | Fix |
|---|---|
| “Select a marquee area first” | There is no marquee. Draw one across the layer, then run the script again. |
| “Open a document first” | No document is open. Open one, select a layer and draw a marquee. |
| “There is no pixel or image layer to sample” | The document has no pixel or image layer, or the selected layer is a shape or text. Select a pixel or image layer. |
| “The marquee is empty” | The marquee has no area. Draw it again. |
| “The marquee does not cover any visible part of the layer” | The marquee is over a transparent area, or the wrong layer is selected. Select the layer you want, or move the marquee over it. |
| The gradient is shorter than the marquee | The marquee ran past the edge of the layer, and the gradient was trimmed to the layer. This is intended. |
| The gradient looks blotchy or speckled | The marquee is crossing fine detail. Make it a little thicker so the colours are averaged, or click Reduce stops. |
| The repeat tick box isn’t there | It only appears when the marquee is 1 or 2 pixels thick. Draw it with the Single Row or Single Column Marquee Tool. |
| The banded preview doesn’t appear on the canvas | Affinity only redraws the area of selected layers while the window is open. Select the layer you are sampling before running the script. The result after OK is not affected. |
| “The banded colour map needs an RGB pixel or image layer” | The layer is CMYK, greyscale or Lab. Convert the document to RGB, or choose Rectangle with gradient. |
| “The marquee doesn’t cover any visible part of the layer, so there is nothing to band” | Area is set to Marquee only and the marquee is over a transparent part of the layer. Set Area to Whole layer, or draw the marquee over the layer. |
| Layers called Gradient Map (B&W) and Gradient Map (Curves) | They were made by an earlier version of the script, which had a smooth gradient map. Delete both if you no longer want them. For smooth gradient maps, use Affinity’s own Gradient Map adjustment. |
| “A temporary mask was left behind” | The script makes a temporary mask to read the marquee. Delete the leftover mask in the Layers panel. |
These buttons change the Adjusted gradient. The Sampled gradient above it stays as it was, so you can compare. The lines under the strip show the number of stops and the changes so far.
Colour
Lighten and Darken
Each click makes every colour 4% lighter or darker. Only the lightness changes, so the colours keep their hue: dark reds get lighter without turning pink or grey.
Saturate and Desaturate
Each click makes the colours 10% stronger or weaker. Desaturate all the way gives greys.
Colours that are already very strong, such as deep reds with no green or blue, can only get richer by getting brighter, so Saturate also lightens them a little.
Hue −15° and Hue +15°
Each click turns every colour 15 degrees around the colour wheel, keeping its lightness and strength. Hue +15° turns red towards orange and yellow, and Hue −15° turns it towards pink and purple. 24 clicks go all the way round, back to the original colours.
Stops
More stops
Fits the gradient to the image more closely, with more stops. Each click goes to the next closer fit that adds stops. It is greyed out when the gradient is already as close as it gets.
Reduce stops
Simplifies the gradient, with fewer stops. Each click goes to the next looser fit that removes stops. Fewer stops give smoother, softer gradients that are easier to edit by hand. It is greyed out at the loosest fit or at 2 stops.
Both buttons rebuild the gradient from the image, not from the current strip, so going back and forth loses nothing. The colour changes above are kept.
Reset
Goes back to the sampled gradient: the starting number of stops, no colour changes and no swatch changes.
Editing the strip directly
You can also edit the Adjusted gradient strip itself, as in Affinity’s gradient editor. Edits made there are used for the rectangle when you click OK, but any button press, swatch change or change to Group colours within rebuilds the strip and replaces them, so make direct edits last. They don’t affect the banded colour map, which uses the swatches.
The stops are grouped by colour, and each group gets a swatch. Change a swatch’s colour to recolour every stop in its group at once, for example to make only the highlights gold. The swatches are also the colours of the banded colour map.
Swatches
There are up to 12 swatches, ordered dark to light. Under each one:
- how many stops are in the group;
- how much of the gradient’s length the group covers, as a percentage;
- a * if you have changed its colour.
The line below the swatches gives the number of groups and how many you have changed.
Group colours within
How alike colours must be to share a swatch, from 5% to 50%. The default is 15%. The percentage is measured the way the eye sees colour, where 100% is the difference between black and white.
- Lower gives more swatches, each covering a narrower range of colours.
- Higher gives fewer, broader swatches. At 50% most gradients have one or two.
If there would be more than 12 groups, the smallest are merged into the nearest ones.
Changing a colour
- Click a swatch. Affinity’s colour picker opens.
- Choose the new colour.
Every stop in the group moves by the same amount as the swatch. The small differences between them stay, so shading in that part of the gradient is kept rather than going flat. The Adjusted gradient strip updates to show the change.
Reset swatch colours
Undoes only the swatch changes. Lighten, Darken, Saturate and Hue changes are kept.
How swatches work with the buttons
- Lighten, Darken, Saturate and Hue apply first, to the whole gradient, and swatch changes apply on top. The swatches update to show the result.
- More stops, Reduce stops, Reset and changing Group colours within rebuild the groups, so swatch changes made before them are cleared. Set the stops and grouping first, then change swatch colours.
Create, in the Output section at the bottom of the window, sets what is added when you click OK.
Create
- Rectangle with gradient: a rectangle filled with the adjusted gradient. This is the default.
- Banded colour map: the sampled layer recoloured in flat bands of the swatch colours. Choosing it shows its settings and a live preview. See Banded Colour Map.
- Both: the banded colour map, with the rectangle placed above it.
Rectangle with gradient
A rectangle called Gradient from marquee, filled with the Adjusted gradient as it is when you click OK, including edits made directly in the strip.
- It is as wide as the part of the marquee that was read, and 150 pixels thick.
- It sits 10 pixels below the marquee, or 10 pixels to its right for a marquee read top to bottom, so each stop starts lined up with the pixels it came from.
- The gradient belongs to the rectangle. Moving the rectangle moves the gradient with it, and resizing it stretches the gradient to fit.
- The gradient stays editable with the Gradient Tool, and you can save it as a swatch from the Swatches panel.
Repeating a 1-pixel line
When the marquee is only 1 or 2 pixels thick, for example drawn with the Single Row or Single Column Marquee Tool, a tick box appears in the Output section:
- Repeat the 1-pixel line down the whole layer, for a horizontal marquee;
- Repeat the 1-pixel line across the whole layer, for a vertical marquee.
It is off by default. Ticked, the result runs the full length of the layer, making stripes:
- Rectangle with gradient: instead of sitting under the marquee, the rectangle covers the layer: the marquee’s width and the layer’s full height for a horizontal marquee, or the marquee’s height and the layer’s full width for a vertical one. The gradient stays lined up with the pixels it came from.
- Banded colour map: the banded line is copied to every row, or every column, of the layer. Each pixel keeps the layer’s own transparency, so holes stay holes. Bands only a few pixels long along the line are merged into their neighbours, so the stripes have no hairlines.
- Both: both are repeated.
A banded colour map recolours the layer you sampled in flat bands of colour, one per swatch: the screen-print or poster look. The darkest parts of the image take the darkest swatch, the lightest parts the lightest swatch, and there is no blending between bands.
Making one
- Set the colours with the buttons and the swatches in Major Colours. Each swatch becomes one band, ordered dark to light.
- Under Create, choose Banded colour map or Both. The band settings appear, and the result is previewed on the canvas.
- Adjust Area, Band split and Edge smoothing.
- Click OK.
It is added as a new pixel layer called Banded colour map, at the top of the layer stack, exactly over the layer you sampled. Transparent areas stay transparent, and your image is not changed.
Previewing it
- The band strip, under Create, shows the bands dark to light, each as wide as its share of the image. The line under it gives the shares, for example “4, dark to light, covering 38% / 26% / 18% / 18%”.
- The canvas shows the result live. It updates as you change the swatches, the grouping, the colour buttons or the band settings. While you are still choosing in the colour picker, the canvas waits until you have picked.
- The preview is not part of your document until you click OK. Choosing Rectangle with gradient again, or Cancel, removes it.
Band settings
Area
- Whole layer: the whole layer you sampled is banded. This is the default.
- Marquee only: only the part inside the marquee is banded, and the rest of the image shows through. The band split points are worked out from the pixels inside the marquee, so the bands fit that area. A feathered marquee gives soft edges to the map.
Band split
Where one band ends and the next begins:
- Matched to image: each band covers a similar share of the picture, with the split points moved slightly so they don’t cut through large, nearly even areas. In a mostly dark photo, more bands go to the dark tones. This is the default.
- Equal lightness steps: the image’s range of lightness, from its darkest to its lightest tones, is cut into equal steps. A mostly dark photo keeps most of its area in the darker bands.
- Equal widths (same share each): each band covers exactly the same share of the picture: with 5 swatches, 20% each. The split points go wherever the shares say, even through even areas.
Edge smoothing
How clean the edges between bands are:
| Setting | What it does |
|---|---|
| Off | Bands follow the image pixel by pixel, with hard, stepped edges. |
| Light | A little smoothing, and smooth edges at any angle. |
| Medium | The default. Grain no longer scatters pixels across band edges, so each edge is one clean line. |
| Strong | Rounder, simpler shapes with less fine detail. |
At every setting, thin slivers of a band, up to about 8 pixels wide, are merged into the band around them, so there are no stray strips such as one down the edge of the layer. Strong merges slivers up to about 16 pixels wide. Wide bands are never changed. Very fine details, such as a highlight 2 or 3 pixels wide, are merged too.
The smoothing is applied to the script’s own copy of the image’s lightness, never to your image.
Good to know
- It is made from the sampled layer only, not from other layers, and it doesn’t update if you change that layer later. Run the script again to remake it.
- It works on RGB layers. On CMYK, greyscale or Lab layers, choose Rectangle with gradient instead.
- It is a pixel layer, so its edges are smooth at the image’s own size but soften if you enlarge it a lot.
- Use the layer’s blend mode and opacity to mix the bands back into the photo, for example Colour or Overlay at a lower opacity.
- For a smooth gradient map rather than bands, use Affinity’s own Gradient Map adjustment, with the colours from the gradient rectangle.