1

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?

Community
  • 1
  • 1
Sawarnik
  • 238
  • 3
  • 11
  • 3
    @KundanSinghChouhan - Opacity? I think you're mistaken... – Jared Farrish Dec 30 '12 at 06:51
  • You probably want to see [Pointy's answer here](http://stackoverflow.com/a/2348659/451969) and manipulate the lightness or value ("brightness") in `hsl`/`hsv`, he provides RGB to HSV or HSL functions. – Jared Farrish Dec 30 '12 at 06:56
  • Here is a method by [Converting RGB value to HSV to change the brigthness](http://stackoverflow.com/questions/5833624/increase-css-brightness-color-on-click-with-jquery-javascript) – Anujith Dec 30 '12 at 06:57
  • i found this http://www.pixastic.com/lib/docs/actions/brightness/ – Huei Tan Dec 30 '12 at 07:11
  • The question linked as "exact duplicate" is very badly answered. – mmgp Dec 30 '12 at 14:19

1 Answers1

2

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:

enter image description here enter image description here enter image description here

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])
mmgp
  • 18,901
  • 3
  • 53
  • 80