
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.
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.
| R | 255 - r |
| G | 255 - g |
| B | 255 - b |
| A | a |
| R | 0.299 * r + 0.587 * g + 0.114 * b |
| G | 0.299 * r + 0.587 * g + 0.114 * b |
| B | 0.299 * r + 0.587 * g + 0.114 * b |
| A | a |
| R | r |
| G | 0 |
| B | 0 |
| A | a |
| R | g |
| G | r |
| B | b |
| A | a |
| R | 0.393 * r + 0.769 * g + 0.189 * b |
| G | 0.349 * r + 0.686 * g + 0.168 * b |
| B | 0.272 * r + 0.534 * g + 0.131 * b |
| A | a |
| R | r > 120 ? 255 : 0 |
| G | g > 160 ? 255 : 0 |
| B | b > 120 ? 255 : 0 |
| A | a |
| R | floor(r / 64) * 85 |
| G | floor(g / 64) * 85 |
| B | floor(b / 64) * 85 |
| A | a |
| R | (r - 128) * 1.5 + 128 |
| G | (g - 128) * 1.5 + 128 |
| B | (b - 128) * 1.5 + 128 |
| A | a |
| R | r > 128 ? 255 - r : r |
| G | g > 128 ? 255 - g : g |
| B | b > 128 ? 255 - b : b |
| A | a |
| R | r + x / width * 600 |
| G | g + y / height * 600 |
| B | b |
| A | a |
| R | r * (1 - 1.6 * hypot(x / width - 0.5, y / height - 0.5) ** 2) |
| G | g * (1 - 1.6 * hypot(x / width - 0.5, y / height - 0.5) ** 2) |
| B | b * (1 - 1.6 * hypot(x / width - 0.5, y / height - 0.5) ** 2) |
| A | a |
| R | r |
| G | g |
| B | b |
| A | a * (1 - y / height) |
255 - r or g * 1.2. The image updates while you type, and errors are shown below the formula.The formulas can use these values of the original pixel:
| Variable | Description |
|---|---|
r, g, b | The red, green and blue values of the original pixel, from 0 to 255. |
a | The alpha (opacity) of the original pixel, from 0 (transparent) to 255 (opaque). |
h, s, l | The 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, y | The position of the pixel: x from 0 (left) to the width minus 1, and y from 0 (top) to the height minus 1. |
width, height | The 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.".
| Field | Description |
|---|---|
| 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.
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:
| Option | Description |
|---|---|
| Clamp | Values lower than 0 become 0, and values greater than 255 become 255. This is the best option for most edits, like r * 1.5. |
| Bounce | The value keeps "bouncing" between 0 and 255: 265 becomes 245, 300 becomes 210, and -20 becomes 20. |
| Remainder | The 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. |
The formulas use the syntax of Javascript expressions.
| Operator | Description | Example |
|---|---|---|
+ - * / | 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 : b | Gives 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 |
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.
| Function | Description | Example |
|---|---|---|
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 |
(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.
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.
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.
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 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.


