Hello all,

Here I have managed to control LED Hue as well as LED saturation but now i would like to add LED brightness or say gamma correction, Here i am not using digital addressable RGB LED i am using just RGB led without any logic gates inside simple R-G-B (3 PWM) from my esp-8266 controller, Here i have done RGB to HSV and then again HSV to RGB to control and to extract brightness from HSV, But unable to manage it wisely , So kindly help me to solve this problem.

Here is my Code:

```
void RGBtoHSV(double r, double g, double b, double light){
Serial.println("r ="+String(r)+ "g ="+String(g)+ "b ="+String(b));
// R, G, B values are divided by 255
// to change the range from 0..255 to 0..1
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;
// h, s, v = hue, saturation, value
double cmax = max(r, max(g, b)); // maximum of r, g, b
double cmin = min(r, min(g, b)); // minimum of r, g, b
double diff = cmax - cmin; // diff of cmax and cmin.
double h = -1, s = -1;
// if cmax and cmax are equal then h = 0
if (cmax == cmin)
h = 0;
// if cmax equal r then compute h
else if (cmax == r)
h = abs(((int)(60.0 * ((g - b) / diff) + 360) %360));
// if cmax equal g then compute h
else if (cmax == g)
h = abs(((int)(60.0 * ((b - r) / diff) + 120) %360));
// if cmax equal b then compute h
else if (cmax == b)
h = abs(((int)(60.0 * ((r - g) / diff) + 240) %360));
// if cmax equal zero
if (cmax == 0)
s = 0;
else
s = (diff / cmax) * 100;
// compute v
double v = cmax * 100;
Serial.println("(" + String(h) + " " + String(s) + " " + String(v) + ")");
HSV_to_RGB(h, s, light);
}
void HSV_to_RGB(double h, double s, double v)
{
double f,p,q,t;
int i,r,g,b;
// h = max((float)0.0, min((float)360.0, h));
// s = max((float)0.0, min((float)100.0, s));
// v = max((float)0.0, min((float)100.0, v));
s /= 100;
v /= 100;
if(s == 0) {
// Achromatic (grey)
r = g = b = abs(v*255);
return;
}
h /= 60; // sector 0 to 5
i = floor(h);
f = h - i; // factorial part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch(i) {
case 0:
r = abs(255*v);
g = abs(255*t);
b = abs(255*p);
break;
case 1:
r = abs(255*q);
g = abs(255*v);
b = abs(255*p);
break;
case 2:
r = abs(255*p);
g = abs(255*v);
b = abs(255*t);
break;
case 3:
r = abs(255*p);
g = abs(255*q);
b = abs(255*v);
break;
case 4:
r = abs(255*t);
g = abs(255*p);
b = abs(255*v);
break;
default: // case 5:
r = abs(255*v);
g = abs(255*p);
b = abs(255*q);
}
Serial.println("r ="+String(r)+ "g ="+String(g)+ "b ="+String(b));
analogWrite(redPin, r);
analogWrite(grnPin, g);
analogWrite(bluPin, b);
}
```