/ src / common / utils / color.h
color.h
 1  #pragma once
 2  
 3  // helper function to get the RGB from a #FFFFFF string.
 4  inline bool checkValidRGB(std::wstring_view hex, uint8_t* R, uint8_t* G, uint8_t* B)
 5  {
 6      if (hex.length() != 7)
 7          return false;
 8      hex = hex.substr(1, 6); // remove #
 9      for (auto& c : hex)
10      {
11          if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')))
12          {
13              return false;
14          }
15      }
16      if (swscanf_s(hex.data(), L"%2hhx%2hhx%2hhx", R, G, B) != 3)
17      {
18          return false;
19      }
20      return true;
21  }
22  
23  // helper function to get the ARGB from a #FFFFFFFF string.
24  inline bool checkValidARGB(std::wstring_view hex, uint8_t* A, uint8_t* R, uint8_t* G, uint8_t* B)
25  {
26      if (hex.length() != 9)
27          return false;
28      hex = hex.substr(1, 8); // remove #
29      for (auto& c : hex)
30      {
31          if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')))
32          {
33              return false;
34          }
35      }
36      if (swscanf_s(hex.data(), L"%2hhx%2hhx%2hhx%2hhx", A, R, G, B) != 4)
37      {
38          return false;
39      }
40      return true;
41  }