Convert HTML Color-code to individual int-values (for r, g , b)

Hi
I am working on an RGB controller and I need to convert a HTML color-code to individual int-values for r, g and b. I get the value(for example "#03fc66") from a colorpicker on a HTML WebServer.

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip(12, 25, NEO_GRB + NEO_KHZ800);

// Replace with your network credentials
const char* ssid = "WLAN";
const char* password = "12345678";

// Create AsyncWebServer object on port 806
AsyncWebServer server(80);

void initSDCard(){
if(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();

if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}

Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
}

void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}

void setup() {
Serial.begin(115200);
strip.begin();
strip.show();

initWiFi();
initSDCard();

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){

 request->send(SD, "/index.html", "text/html");

int paramsNr = request->params();
Serial.println(paramsNr);

for(int i=0;i<paramsNr;i++){

    AsyncWebParameter* p = request->getParam(i);
    Serial.print("Param name: ");
    Serial.println(p->name());
    Serial.print("Param value: ");
    Serial.println(p->value());
    Serial.println("------");

    

    Serial.print("Setting Neopixel...");




}

});

server.serveStatic("/", SD, "/");

server.begin();
}

void loop() {

}

I need to convert the "p->value()"

Thanks for every help in advance!

`

what gets printed now?
have you tried

    Serial.println (p->value, HEX);

if i use your line i get this error: no matching function for call to 'HardwareSerial::println(const String&, int)'

now i tried updating my code to:

server.on("/", HTTP_GET, [](AsyncWebServerRequest request){
request->send(SD, "/index.html", "text/html");
int paramsNr = request->params();
Serial.println(paramsNr);
for(int i=0;i<paramsNr;i++){
AsyncWebParameter
p = request->getParam(i);
Serial.print("Param name: ");
Serial.println(p->name());
Serial.print("Param value: ");
Serial.println(p->value());
Serial.println("------");
Serial.println(p->value().c_str());
int number = (int)strtol&((p->value().c_str())[1], NULL, 0);
Serial.println(number);
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;
// DEBUG
Serial.print("RGB: ");
Serial.print(r, DEC);
Serial.print(" ");
Serial.print(g, DEC);
Serial.print(" ");
Serial.print(b, DEC);
Serial.println(" ");
}
});

i tried printing out the received color code value and it is working. But i cant convert the color code into indipendent grb values...

for the rgb values i always get 0 0 0...
Maybe something is with the conversion to a nint value wrong...?(int number = (int)strtol&((p->value().c_str())[1], NULL, 0);
)
grafik

Your strtol line is flawed, the & operator is in the wrong place. Try

int number = (int)strtol(&(p->value().c_str())[1], NULL, 0);

Also, is int four bytes on your processor? If not you need to be using long.

  unsigned long n = strtol( p->value().c_str()+1, nullptr, 16 );
  Serial.println(n, HEX);
  byte r = n >> 16;
  byte g = n >> 8;
  byte b = n;
  Serial.print("r = "); Serial.println(r, HEX);
  Serial.print("g = "); Serial.println(g, HEX);
  Serial.print("b = "); Serial.println(b, HEX);

I would have thought that the 'base' should be 16 for hex. A base of 0 would mean the string would have to contain "0x03fc66" and not "#03fc66".

Agreed.

Thank you so much!!!!!

thank you!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.