Convert a hex color string to orxColor

Code

  std::string sColor = "#ff8080ff" or "0xff8080ff"; 
 
  // Erase '#' from it, if it's in 0x format, do nothing 
  if(sColor.length() != 0 && sColor.at(0) == '#')
    sColor.erase(0,1);
 
  // RGBA byte order, this could be rgba or argb or bgra
  std::string byteOrder = "rgba";
 
  unsigned long hexValue=0;
  // convert hex string to unsigned long, strtoul takes care of '0x' format
  hexValue = std::strtoul(sColor.c_str(),NULL,16);
 
  int n =  byteOrder.length() - 1 ;
 
  orxCOLOR color;
  color.vRGB.fR = ((hexValue >> (n - byteOrder.find('r')) * 8) & 0xFF) / 255.0;  // Extract the RR byte
  color.vRGB.fG = ((hexValue >> (n - byteOrder.find('g')) * 8) & 0xFF) / 255.0;  // Extract the GG byte
  color.vRGB.fB = ((hexValue >> (n - byteOrder.find('b')) * 8) & 0xFF) / 255.0;  // Extract the BB byte
  color.fAlpha  = ((hexValue >> (n - byteOrder.find('a')) * 8) & 0xFF) / 255.0;  // Extract the AA byte