invert a hex color

Invert Hex Color Code

By Nicole Allen
AD

imagine you have a hex color, let's say #FF0000 (red), and you want to turn it into its darkest shade. You could use Photoshop, Illustrator, or an online color converter. But what if you're a web developer who wants to do this programmatically? You can invert hex colors without leaving the comfort of your coding environment.

The process involves converting the hex color to its RGB equivalent, inverting each of the RGB values, and then converting that back to hex. Let's dive into how to do this with JavaScript, as it's widely supported and perfect for web development tasks.

Understanding Hex Colors

Hex colors are six-digit codes that define a color. They start with a hash symbol (#) followed by three pairs of hexadecimal digits (ranging from 0-9 and A-F), each representing the intensity of the red, green, and blue (RGB) components of the color.

AD
Pick Colors from Any Image – Free HEX, RGB & Color Codes
Pick Colors from Any Image – Free HEX, RGB & Color Codes
All the Hex Codes for Colors You Need to Succeed Online | LouiseM
All the Hex Codes for Colors You Need to Succeed Online | LouiseM
Color Combinaisons: Palette for Graphic Design #67
Color Combinaisons: Palette for Graphic Design #67

For instance, in #FF0000, 'FF' is the intensity of red at its maximum, '00' is the intensity of green at its minimum, and '00' is the intensity of blue at its minimum. This gives us the color red.

Converting Hex to RGB

To invert a hex color, we first need to convert it to RGB. Here's a simple JavaScript function to do that:

Pastels HEX Color Chart Printable | Color Reference Poster | Color Palette Cheat Sheet | Digital Wall Art | Instant Digital Download
Pastels HEX Color Chart Printable | Color Reference Poster | Color Palette Cheat Sheet | Digital Wall Art | Instant Digital Download
Free Wedding Color Palette Generator: hex codes in seconds
Free Wedding Color Palette Generator: hex codes in seconds
the color palette is dark and purple, but it's not very noticeable to see
the color palette is dark and purple, but it's not very noticeable to see

function hexToRgb(hex) {
var bigint = parseInt(hex.slice(1), 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return [r, g, b];
}

Converting RGB to Hex

Once we've inverted the RGB values, we need to convert them back to hex. Here's a function to do that:

AD

function rgbToHex(rgbArr) {
return "#" + ((1 << 24) + (rgbArr[0] << 16) + (rgbArr[1] << 8) + rgbArr[2]).toString(16).slice(1);
}

Color Combinaisons: Palette for Graphic Design #57
Color Combinaisons: Palette for Graphic Design #57
Color Combinaisons: Palette for Graphic Design #32
Color Combinaisons: Palette for Graphic Design #32
Color Palette | Moodboard | Color
Color Palette | Moodboard | Color
the color scheme for an art project is purple and pink, with different colors on it
the color scheme for an art project is purple and pink, with different colors on it
a green and white cell phone screen with the text hex on it
a green and white cell phone screen with the text hex on it
an image of the menu for a restaurant or bar with different colors and shapes on it
an image of the menu for a restaurant or bar with different colors and shapes on it

Inverting Hex Colors

Now that we have the necessary conversions, let's write a function to invert a hex color. This involves subtracting each RGB value from its maximum (255) and then converting the result back to hex:

function invertHex(hexColor) {
var rgb = hexToRgb(hexColor);
var invertedRgb = rgb.map(function(value) { return 255 - value; });
return rgbToHex(invertedRgb);
}

AD

Example of inversion

For instance, if we call invertHex("#FF0000"), it will return "#0000FF", which is the darkest shade of red, i.e., blue.

Color Palette | Moodboard | Color
Color Palette | Moodboard | Color
Color Combinaisons: Palette for Graphic Design #30
Color Combinaisons: Palette for Graphic Design #30
two people standing next to each other in front of a window with red light coming from it
two people standing next to each other in front of a window with red light coming from it
Colortone to HEX Converter
Colortone to HEX Converter
an image of a tree in the middle of a forest with color swats on it
an image of a tree in the middle of a forest with color swats on it
Hex codes
Hex codes
Color Combinaisons: Palette for Graphic Design #105
Color Combinaisons: Palette for Graphic Design #105
color palette | blending
color palette | blending
Color Ideas
Color Ideas
an image of different colors and numbers on the same page, with one color in each column
an image of different colors and numbers on the same page, with one color in each column
Color Combinaisons: Palette for Graphic Design #90
Color Combinaisons: Palette for Graphic Design #90
Celestial Aesthetic Color Palette | Indigo Blue & Gold HEX Codes
Celestial Aesthetic Color Palette | Indigo Blue & Gold HEX Codes
Pantone color 2026 🔥
Pantone color 2026 🔥

invertHex in action

This function will work with any hex color. For example, if we invert the color purple (#800080), it will return "#800000", which is the darkest shade of purple, i.e., red.

With this simple JavaScript function, you can easily invert hex colors for a myriad of web development tasks, from creating dark mode color schemes to generatingcolor combinations for your projects. Happy coding!

AD
AD