hipersayanX / ColorScale

Color scale algorithm and B&W image coloring

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ColorScale

Color scale algorithm and B&W image coloring.

Description

The algorithm consists in dividing a gray scale by sectors, replace every sector by a new color gradient, and then converting a gray scale image to the new scale. The user defines the contol points by it's colors, and the algorithm redistribute to control points arround the grey scale. The grey scale is divided in nControlPoints - 1.
Also, this algorithm can be used for reconstructing the color of a grey scaled image by using it's most representative colors.

Implementation

Supposing a RGBA space with {Rbits, Gbits, Bbits[, Abits]} components, the luminance of a color can be calculated as:

Luma = (R + G + B) / 3

The resolution of the luminance value can be calculated as:

LumaBits = max(Rbits, Gbits, Bbits)

The maximum value of the luminance can be calculated as:

LumaMax = 2 ^ LumaBits - 1

An image is composed of many pixels, and a pixel is defined by it color.
So the maximun number of possible tones for a black and white image is:

nLumaColors = LumaMax + 1

A grey scale table can be created using each grey tone.
We can create a color table defined by control ponits, and each control point is defined by a color. The control points are distributted monotonically (or not) and extrapolated to the grey scale table.
If nColors is the number of control points, we can split the grey scale table in nColors - 1 sectors. We can replace each grey scale sector by the sector defined by the corresponding control points.
The ecuations for the linear transform are:

G = k * (getUpperColor(GreyTable, sector) - getLowerColor(GreyTable, sector)) + getLowerColor(GreyTable, sector)
C = k * (ColorTable[sector + 1] - ColorTable[sector]) + ColorTable[sector]

Examples

Here you can see some simple color transforms.

# Replace the grey color by orange.
colorTable = [[  0,   0,   0],
              [225, 127,   0],
              [255, 255, 255]]

Replacing the grey color by orange

# Rainbow
colorTable = [[255,   0, 255],
              [  0,   0, 255],
              [  0, 255, 255],
              [  0, 255,   0],
              [255, 255,   0],
              [255,   0,   0]]

Rainbow gradient example

# Hot colors.
colorTable = [[  0,   0,   0],
              [255,   0,   0],
              [255, 255,   0],
              [255, 255, 255]]

Using a red-yellow color palette

# Soft colors
colorTable = [[127,   0, 127],
              [255, 191, 255]]

Replace the outermost colors by a contrastless colors

# Printing blue
colorTable = [[  0,   0,   0],
              [  0,   0,   0],
              [  0, 127, 255],
              [  0, 127, 255],
              [127, 191, 255],
              [127, 191, 255]]

Extending outermost and middle colors

# Wrong luminance sorting
colorTable = [[  0,   0,   0],
              [  0, 255, 255],
              [  0,   0, 255],
              [255, 255, 255]]

Switching the color luminance in wrong order

Color reconstruction

The idea is very simple, as said before, each color has a luminance value, the luminance value isn't a unique value, many colors shares the same luminance value, but in some cases when you see a black and white picture you can guess what is the original color of an object depending on his luminance and the context of the scene.
So, the first step before you can recolor a B&W image is to split the grey scale palette into a few characteristic colors.

colorTable = [[  0,   0,   0],
              [255,   0, 255],
              [  0,   0, 255],
              [  0, 255, 255],
              [  0, 255,   0],
              [255, 255,   0],
              [255,   0,   0],
              [255, 255, 255]]

Color split

And then, replace every color by it's supposed original color.

colorTable = [[  0,   0,   0],
              [ 40,  36,  33],
              [ 41,  71, 105],
              [ 69, 108, 151],
              [ 92, 145, 199],
              [171, 195, 181],
              [209, 226, 220],
              [255, 255, 255]]

Manual color recontruction

It's also possible to automatically reconstruct the original colors if you have the original color palette.
The createColorTable(image, nColors) function will create a color table with each color available in image sorted by luminance and usage, then will reduce the number of colors to the most used colors without repeating the luminance value. If a luminance value is missing this will be filled whith a grey value with the same luminance.
The number of colors in this palette will be the number of grey tones in the image color depth, ie. in a RGB888 image the numbre of colors will be 256.
Finally, this number of colors will be reduced to nColors, where each color is ditributed uniformly.

Automatic color recontruction

The algorithm not always works as expect. If there are two or more colors with the same luminance and one of those prevails over the others, that color will hide the original tone.

Wrong color recontruction

Questions?

Just check out the source code, this is more understandable than my explanation :P

Content attribution

1 2 3 4 5 6 7 8 9

About

Color scale algorithm and B&W image coloring

License:GNU General Public License v3.0


Languages

Language:Python 100.0%