I recently found a solution to dynamically update the color of any product image. So with just one
<img> of a product, we can colorize it in different ways to show different color options. We don’t even need any fancy SVG or CSS to get it done!
We’ll be using an image editor (e.g. Photoshop or Sketch) and the image transformation service imgix. (This isn’t a sponsored post and there is no affiliation here — it’s just a technique I want to share.)
See the Pen
Dynamic Car color by Der Dooley (@ddools)
I work with a travel software company called CarTrawler on the engineering team, and I recently undertook a project a revamp our car images library that we use to display car rental search results. I wanted to take this opportunity to introduce dynamically colored cars.
We may sometimes load up to 200 different cars at the same time, so speed and performance are key requirements. We also have five different products throughout unique code bases, so avoiding over-engineering is vital to success.
I wanted to be able to dynamically change the color of each of these cars without needing additional front-end changes to the code.
Step 1: The Base Layer
I’m using car photos here, but this technique could be applied to any product. First we need a base layer. This is the default layer we would display without any color and it should look good on its own.
Step 2: The Paint Layer
Next we create a paint layer that is the same dimensions as the base layer, but only contains the areas where the colors should change dynamically.
A light color is key for the paint layer. Using white or a light shade of gray gives us a great advantage because we are ultimately “blending” this image with color. Anything darker or in a different hue would make it hard to mix this base color with other colors.
Step 3: Using the imgix API
This is where things get interesting. I’m going to leverage multiple parameters from the imgix API. Let’s apply a black to our paint layer.
We changed the color by applying a standard black hex value of
If you noticed the URL of the image above, you might be wondering: What the heck are all those parameters? The imgix API docs have a lot of great information, so no need to go into greater detail here. But I will explain the parameters I used.
w. The width I want the image to be
bri. Adjusts the brightness level
con. Adjusts the amount of contrast
monochrome. The dynamic hex color
Because we are going to stack our layers via imgix we will need to encode our paint layer. That means replacing some of the characters in the URL with encoded values — like we’d do if we were using inline SVG as a background image in CSS.
Step 4: Stack the Layers
Now we are going to use imgix’s watermark parameter to stack the paint layer on top of our base layer.
Let’s look at the parameters being used:
w. This is the image width and it must be identical for both layers.
mark-align. This centers the paint layer on top of the base layer.
mark. This is where the encoded paint layer goes.
In the end, you will get a single URL that will look like something like this:
That gives the car in black:
Now that we have one URL, we can basically swap out the black hex value with any other colors we want. Let’s try blue!
Why not red?
That’s it! There are certainly other ways to accomplish the same thing, but this seems so straightforward that it’s worth sharing. There was no need code a bunch of additional functionality. No complex libraries to manage or wrangle. All we need is a couple of images that an online tool will stack and blend for us. Seems like a pretty reasonable solution!
Thanks for the post! I have a few questions:
1) How did you create the Paint Layer in step #2? Was there a way to do that with the service or did you do that manually in PhotoShop?
2) How much does the service charge you for the colorization service?
3) I’ve been researching how to do something similar and it seems that the service offered from LiquidPixels is much more robust, easier to use, and has many more features than the one you used. You should check them out at http://www.liquidpixels.com
but the ability to edit on the fly/ watermark/blend colors is at no additional costs
Neat API! Here’s another version of recoloring products with 1 image and CSS (with a bonus color picker to choose any color) – inspired by this article :)
Thanks for the inspiration to write an article and learn some CSS variable action!
Love it, nice alternative approach
Wow! It’s realy amazing! I like it! Thanks for sharing!
Okay, I have to find a good excuse to use this on my site. It’s so cool!!