Convert from HEX color to RGB struct in C

前端 未结 5 1075
忘了有多久
忘了有多久 2020-12-15 11:07

How do you convert from color HEX code to RGB in pure C using C library only (without C++ or templates)? The RGB struct may be like this:



        
5条回答
  •  臣服心动
    2020-12-15 11:36

    Assuming that your hex value is a 32-bit 'int' type, and that we use the RGB struct described above, then maybe do something like:

    struct RGB colorConverter(int hexValue)
    {
      struct RGB rgbColor;
      rgbColor.r = ((hexValue >> 16) & 0xFF) / 255.0;  // Extract the RR byte
      rgbColor.g = ((hexValue >> 8) & 0xFF) / 255.0;   // Extract the GG byte
      rgbColor.b = ((hexValue) & 0xFF) / 255.0;        // Extract the BB byte
    
      return rgbColor; 
    }
    

提交回复
热议问题