kakopappa / sinric

Amazon Alexa Smart home skill / Google Home Action for ESP8266 / ESP32 / Arduino
https://sinric.com
285 stars 166 forks source link

How to get HEX/RGB value from SetColor? #373

Closed Derrin616 closed 4 years ago

Derrin616 commented 4 years ago

Hi, How can i get the HEX Value or RGB?

Code :

    else if(action == "SetColor") {
      if (deviceId == "ttt")
      {
        String hue = json ["value"]["hue"];
        String saturation = json ["value"]["saturation"];
        String brightness = json ["value"]["brightness"];

        Serial.println("[WSc] hue: " + hue);
        Serial.println("[WSc] saturation: " + saturation);
        Serial.println("[WSc] brightness: " + brightness);
        }

}

Derrin616 commented 4 years ago

Fixed!

void HSBtoRGB(int hue, int sat, int bright, byte rgb[]) {

// constrain all input variables to expected range hue = constrain(hue, 0, 360); sat = constrain(sat, 0, 100); bright = constrain(bright, 0, 100);

// define maximum value for RGB array elements float max_rgb_val = 255;

// convert saturation and brightness value to decimals and init r, g, b variables float sat_f = float(sat) / 100.0; float bright_f = float(bright) / 100.0;
int r, g, b;

// If brightness is 0 then color is black (achromatic)
// therefore, R, G and B values will all equal to 0
if (bright <= 0) {      
    rgb[0] = 0;
    rgb[1] = 0;
    rgb[2] = 0;
} 

// If saturation is 0 then color is gray (achromatic) // therefore, R, G and B values will all equal the current brightness if (sat <= 0) {
rgb[0] = bright_f max_rgb_val; rgb[1] = bright_f max_rgb_val; rgb[2] = bright_f * max_rgb_val; }

// if saturation and brightness are greater than 0 then calculate 

// R, G and B values based on the current hue and brightness else {

    if (hue >= 0 && hue < 120) {
  float hue_primary = 1.0 - (float(hue) / 120.0);
  float hue_secondary = float(hue) / 120.0;
  float sat_primary = (1.0 - hue_primary) * (1.0 - sat_f);
  float sat_secondary = (1.0 - hue_secondary) * (1.0 - sat_f);
  float sat_tertiary = 1.0 - sat_f;
  r = (bright_f * max_rgb_val) * (hue_primary + sat_primary);
  g = (bright_f * max_rgb_val) * (hue_secondary + sat_secondary);
  b = (bright_f * max_rgb_val) * sat_tertiary;  
    }

    else if (hue >= 120 && hue < 240) {
  float hue_primary = 1.0 - ((float(hue)-120.0) / 120.0);
  float hue_secondary = (float(hue)-120.0) / 120.0;
  float sat_primary = (1.0 - hue_primary) * (1.0 - sat_f);
  float sat_secondary = (1.0 - hue_secondary) * (1.0 - sat_f);
  float sat_tertiary = 1.0 - sat_f;
  r = (bright_f * max_rgb_val) * sat_tertiary;  
  g = (bright_f * max_rgb_val) * (hue_primary + sat_primary);
  b = (bright_f * max_rgb_val) * (hue_secondary + sat_secondary);
    }

    else if (hue >= 240 && hue <= 360) {
  float hue_primary = 1.0 - ((float(hue)-240.0) / 120.0);
  float hue_secondary = (float(hue)-240.0) / 120.0;
  float sat_primary = (1.0 - hue_primary) * (1.0 - sat_f);
  float sat_secondary = (1.0 - hue_secondary) * (1.0 - sat_f);
  float sat_tertiary = 1.0 - sat_f;
  r = (bright_f * max_rgb_val) * (hue_secondary + sat_secondary);
  g = (bright_f * max_rgb_val) * sat_tertiary;  
  b = (bright_f * max_rgb_val) * (hue_primary + sat_primary);
    }      
    rgb[0]=r;
    rgb[1]=g;
    rgb[2]=b;

}

}

                    // Alexa, set the device name to red
        // get text: {"deviceId":"xxxx","action":"SetColor","value":{"hue":0,"saturation":1,"brightness":1}}
        String hue = json ["value"]["hue"];
        String saturation = json ["value"]["saturation"];
        String brightness = json ["value"]["brightness"];

int H=hue.toInt(); float S=saturation.toFloat()100.0; float B=brightness.toFloat()100.0; byte rgb[3]; HSBtoRGB(H,S,B,rgb); Serial.println(rgb[0]); Serial.println(rgb[1]); Serial.println(rgb[2]);