Possible Duplicate:
Increase CSS brightness color on click with jquery/javascript?
In JavaScript, how I can change a color's brightness, when we know the values of red, blue ,green much like it can be done in MSPaint?
Possible Duplicate:
Increase CSS brightness color on click with jquery/javascript?
In JavaScript, how I can change a color's brightness, when we know the values of red, blue ,green much like it can be done in MSPaint?
The question is unrelated to Javascript, and I don't know what is the effect you are relating to MSPaint. Plus, converting between color spaces just to adjust brightness might be too expensive. There is a much simpler approach that might be good enough for you, which is a gamma correction that is fast and simple to perform. If your values of red, green, and blue are in the range [0, 255] you can also easily create a lookup-table (lut
) which will be used to quickly apply the gamma correction for a given value in the range [0, 255]. The pseudocode for this method is:
GAMMA = k
lut = []
for i = 0 to 255
v = i/255.0
lut.push(round(255 * (v ^ GAMMA)))
foreach x, y in image
image[x, y] = lut[image[x, y]]
A value of k
greater than 1 will brighten the image, and lower than 1 will darken the image. The gamma correction is applied to each pixel component (i.e., red, green, and blue individually). Here are some examples with k = 1, 2, 3
, respectively:
The full Python code to perform this (might help in the Javascript translation):
import sys
from PIL import Image
GAMMA = 1/3.0
lut = []
for i in xrange(256):
v = i/255.
lut.append(round(255 * (v ** GAMMA)))
img = Image.open(sys.argv[1])
img = img.point(lambda x: lut[x])
img.save(sys.argv[2])