tb_color.cpp 1.1 KB

12345678910111213141516171819202122232425262728
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_color.h"
  6. #include <stdio.h>
  7. namespace tb {
  8. // == TBColor ===============================================================================
  9. void TBColor::SetFromString(const char *str, int len)
  10. {
  11. int r, g, b, a;
  12. if (len == 9 && sscanf(str, "#%2x%2x%2x%2x", &r, &g, &b, &a) == 4) // rrggbbaa
  13. Set(TBColor(r, g, b, a));
  14. else if (len == 7 && sscanf(str, "#%2x%2x%2x", &r, &g, &b) == 3) // rrggbb
  15. Set(TBColor(r, g, b));
  16. else if (len == 5 && sscanf(str, "#%1x%1x%1x%1x", &r, &g, &b, &a) == 4) // rgba
  17. Set(TBColor(r + (r << 4), g + (g << 4), b + (b << 4), a + (a << 4)));
  18. else if (len == 4 && sscanf(str, "#%1x%1x%1x", &r, &g, &b) == 3) // rgb
  19. Set(TBColor(r + (r << 4), g + (g << 4), b + (b << 4)));
  20. else
  21. Set(TBColor());
  22. }
  23. }; // namespace tb