RGB Formula Editor

Edit images with math: type a formula for the red, green, blue and alpha values of every pixel.
A photo transformed with different RGB formulas

RGB Formula Editor is a free online tool to edit images using math formulas. Every pixel of an image has a red, a green and a blue value, from 0 to 255, plus an alpha value for the transparency. You type a formula for each of them, and the formula is computed for every pixel of the image.

Simple formulas do a lot: 255 - r inverts the colors, g in the red channel swaps two channels, r > 128 ? 255 : 0 turns a channel into black and white, and a * (1 - y / height) fades the image into transparency. The formulas can use the original color of the pixel (also as hue, saturation and lightness), its position and math functions.

The formulas are evaluated by your graphics card (with WebGL2), so the image updates while you type, even for large photos. Everything runs in your browser: your image is never uploaded to a server. To change the hue, the saturation and the lightness instead, use the HSL Formula Editor.

Drop your image here
You can also paste an image with Ctrl+V.

Examples

Click on any example to apply its formulas to your image (or to the sample image, if you haven't opened one), and edit them.

Original image
The sample image, before applying any formula.
Invert colors
Every channel is subtracted from 255, which gives the negative of the image.
R255 - r
G255 - g
B255 - b
Aa
Grayscale
The same value for the three channels. The weights follow the brightness that our eyes see: green looks much brighter than blue.
R0.299 * r + 0.587 * g + 0.114 * b
G0.299 * r + 0.587 * g + 0.114 * b
B0.299 * r + 0.587 * g + 0.114 * b
Aa
Red channel
Only the red channel is kept, so the image shows how much red each pixel has.
Rr
G0
B0
Aa
Channel swap
The red and green channels are swapped: the grass becomes reddish and the lips become green.
Rg
Gr
Bb
Aa
Sepia
Each channel is a mix of the three original channels, which gives the brown tones of old photos.
R0.393 * r + 0.769 * g + 0.189 * b
G0.349 * r + 0.686 * g + 0.168 * b
B0.272 * r + 0.534 * g + 0.131 * b
Aa
Threshold
Each channel becomes 0 or 255, so the image has only 8 colors.
Rr > 120 ? 255 : 0
Gg > 160 ? 255 : 0
Bb > 120 ? 255 : 0
Aa
Posterize
Each channel is rounded down to one of 4 levels (0, 85, 170 and 255), so the image has flat areas of color.
Rfloor(r / 64) * 85
Gfloor(g / 64) * 85
Bfloor(b / 64) * 85
Aa
More contrast
The values are moved away from the middle (128): dark pixels become darker and light pixels become lighter.
R(r - 128) * 1.5 + 128
G(g - 128) * 1.5 + 128
B(b - 128) * 1.5 + 128
Aa
Solarize
Only the light values are inverted, like a photo exposed to light while it is developed.
Rr > 128 ? 255 - r : r
Gg > 128 ? 255 - g : g
Bb > 128 ? 255 - b : b
Aa
Color bands
The red grows from left to right and the green from top to bottom. With "Bounce", the values that pass 255 come back down, which makes bands of color.
Rr + x / width * 600
Gg + y / height * 600
Bb
Aa
Vignette
The pixels become darker as they get farther from the center of the image.
Rr * (1 - 1.6 * hypot(x / width - 0.5, y / height - 0.5) ** 2)
Gg * (1 - 1.6 * hypot(x / width - 0.5, y / height - 0.5) ** 2)
Bb * (1 - 1.6 * hypot(x / width - 0.5, y / height - 0.5) ** 2)
Aa
Fade out
The alpha decreases from top to bottom, so the image fades into transparency.
Rr
Gg
Bb
Aa * (1 - y / height)

How to edit an image with RGB formulas?

  1. Upload your image - Drop a JPG, PNG or WebP image in the box above, or paste an image with Ctrl+V, or click "Try it with a sample image".
  2. Type the formulas - One for the new red (R), one for the new green (G), one for the new blue (B) and one for the new alpha (A). The formulas can use the original values of the pixel, like 255 - r or g * 1.2. The image updates while you type, and errors are shown below the formula.
  3. Start from an example (optional) - Click on one of the examples above to load its formulas, and change them.
  4. Choose what happens to values outside the range - "Clamp" (recommended) keeps the red, green and blue values between 0 and 255. "Bounce" and "remainder" make bands of colors.
  5. Download the image - Click "Download image" to save the result as a PNG image, with its transparency.

Documentation

Variables

The formulas can use these values of the original pixel:

VariableDescription
r, g, bThe red, green and blue values of the original pixel, from 0 to 255.
aThe alpha (opacity) of the original pixel, from 0 (transparent) to 255 (opaque).
h, s, lThe hue (0 to 360), the saturation (0 to 100) and the lightness (0 to 100) of the original pixel. For example, l * 2.55 in the three channels gives a grayscale image.
x, yThe position of the pixel: x from 0 (left) to the width minus 1, and y from 0 (top) to the height minus 1.
width, heightThe size of the image, in pixels. x / width goes from 0 on the left to 1 on the right.

You can also use the constants PI (3.14159...) and E (2.71828...), with or without "Math.".

Formulas

FieldDescription
R (red)The new red value of the pixel. It should give a value between 0 and 255.
G (green)The new green value of the pixel. It should give a value between 0 and 255.
B (blue)The new blue value of the pixel. It should give a value between 0 and 255.
A (alpha)The new alpha of the pixel, from 0 (transparent) to 255 (opaque). Values outside this range are clamped. Use a to keep the original transparency.

Values with decimals are rounded to the nearest whole number.

Values outside the range

When the formula of the red, the green or the blue gives a value outside the range from 0 to 255, the value is adjusted:

OptionDescription
ClampValues lower than 0 become 0, and values greater than 255 become 255. This is the best option for most edits, like r * 1.5.
BounceThe value keeps "bouncing" between 0 and 255: 265 becomes 245, 300 becomes 210, and -20 becomes 20.
RemainderThe value starts over from 0 when it passes 255: 265 becomes 10, and -20 becomes 235. Values between 0 and 255 are kept, so white stays white.

Operators

The formulas use the syntax of Javascript expressions.

OperatorDescriptionExample
+ - * /Addition, subtraction, multiplication and division. Multiplication always needs the *: write 2 * r, not 2r.(r + g + b) / 3
**Power.255 * (r / 255) ** 2
%Remainder of the division.x % 20 < 10 ? r : 0
< > <= >= == !=Comparisons. They give 1 when true and 0 when false.(r > 128) * 255
condition ? a : bGives the first value when the condition is true (not zero) and the second value otherwise.r > 128 ? 255 - r : r
&& || !Logical AND, OR and NOT, to combine conditions.g > r && g > b ? 0 : a
& | ^ ~ << >> >>>Bitwise AND, OR, XOR, NOT and shifts. The values are converted to whole numbers first. r & 192 keeps only the 2 highest bits of the red value.r ^ x

Math functions

The functions of the Javascript Math object can be used with or without "Math.": Math.sin(x) and sin(x) are the same. The angles are in radians.

FunctionDescriptionExample
min(a, b, ...), max(a, b, ...)The smallest and the largest of the values.max(r, g, b)
abs(a), sign(a)Absolute value, and the sign (-1, 0 or 1).abs(r - g)
floor(a), ceil(a), round(a), trunc(a)Round down, round up, round to the nearest whole number, and remove the decimals.floor(r / 64) * 85
sin(a), cos(a), tan(a)Sine, cosine and tangent. Also asin, acos, atan and atan2(y, x).r + 60 * sin(y / 10)
hypot(a, b, ...)The square root of the sum of the squares: hypot(x - width / 2, y - height / 2) is the distance from the center of the image.r - hypot(x - width / 2, y - height / 2) / 4
sqrt(a), cbrt(a), pow(a, b)Square root, cube root and power.sqrt(r * 255)
exp(a), log(a), log2(a), log10(a)Exponential and logarithms. Also expm1(a) and log1p(a).log1p(r) * 46
sinh(a), cosh(a), tanh(a), asinh(a), acosh(a), atanh(a)Hyperbolic functions and their inverses. tanh makes smooth S-curves, to add contrast.128 + 128 * tanh((r - 128) / 64)
random()A random number between 0 and 1, different for every pixel. The same image always gets the same numbers.r + (random() - 0.5) * 40

Frequently Asked Questions (FAQ)

Why does (r + g + b) / 3 give a strange grayscale?

The average treats the three channels the same, but our eyes see green as much brighter than red, and red as brighter than blue. A grayscale based on the perceived brightness gives more weight to green: 0.299 * r + 0.587 * g + 0.114 * b, as in the "Grayscale" example.

Can I write Javascript code in the formulas?

The formulas use the syntax of Javascript expressions, with the operators and the Math functions listed above. Statements, like let, if or loops, can't be used: use the condition ? a : b operator instead of if.

How can I make part of the image transparent?

Use a condition in the alpha formula. For example, r > 240 && g > 240 && b > 240 ? 0 : a makes the white pixels transparent. Download the image as PNG to keep the transparency. To remove a background automatically, use the Background remover.

What is the difference with the HSL Formula Editor?

This tool changes the red, green and blue values of every pixel, which is ideal for channel swaps, inversions, thresholds and color filters. The HSL Formula Editor changes the hue, the saturation and the lightness, which is easier for color shifts, saturation changes, colorizing and selecting a range of colors.

Why do my formulas give slightly different values than a calculator?

The formulas are computed by your graphics card, with 32-bit numbers. For colors this makes no visible difference, but very large values (for example sin(x * y * 1000)) lose precision.

The image is too large. What can I do?

The size of the image is limited by your graphics card, usually to 8192 or 16384 pixels on each side. Make it smaller with Resize image first.

What is WebGL2?

WebGL2 is the technology that lets websites use your graphics card. All modern browsers support it. If the image doesn't appear, check that hardware acceleration is enabled in the settings of your browser.

Is my image uploaded to a server?

No. Your image is read and edited by your own browser, and it never leaves your computer.

Related tools

HSL Formula Editor

Edit images using math equations for the hue, saturation and lightness of each pixel.

CMYK Formula Editor

Edit images using math equations for the cyan, magenta, yellow and black ink of each pixel.

Change image color

Change color of images by changing the red, green and blue components of each pixel of the image.

Change image hue

Adjust the hue of images. Hue rotate images by specifying an angle around the color circle.
Create images using math equations

Create images using math equations

Generate images from mathematical formulas.
Create animations using math equations

Create animations using math equations

Create animations from mathematical formulas, with the time as a variable.
Droste Effect Generator

Droste Effect Generator

Put a picture inside itself, again and again: create the recursive Droste effect from any image.

Convolution Matrix

Apply a convolution matrix to images to blur, sharpen, emboss or detect edges, with ready-made or custom kernels.