color.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include <string>
  3. #include <cstdlib>
  4. namespace lumacs {
  5. /// RGB Color representation
  6. struct Color {
  7. int r, g, b;
  8. Color(int r = 0, int g = 0, int b = 0) : r(r), g(g), b(b) {}
  9. /// Convert to ncurses color index (will be assigned dynamically)
  10. int to_ncurses_color() const;
  11. /// Create Color from hex string (e.g., "#FF5500" or "FF5500")
  12. static Color from_hex(const std::string& hex) {
  13. std::string h = hex;
  14. // Remove leading # if present
  15. if (!h.empty() && h[0] == '#') {
  16. h = h.substr(1);
  17. }
  18. // Parse hex values
  19. if (h.length() >= 6) {
  20. int r = static_cast<int>(std::strtol(h.substr(0, 2).c_str(), nullptr, 16));
  21. int g = static_cast<int>(std::strtol(h.substr(2, 2).c_str(), nullptr, 16));
  22. int b = static_cast<int>(std::strtol(h.substr(4, 2).c_str(), nullptr, 16));
  23. return Color(r, g, b);
  24. }
  25. return Color(0, 0, 0); // Default to black on parse error
  26. }
  27. /// Comparison operator for use in maps
  28. bool operator<(const Color& other) const {
  29. if (r != other.r) return r < other.r;
  30. if (g != other.g) return g < other.g;
  31. return b < other.b;
  32. }
  33. bool operator==(const Color& other) const {
  34. return r == other.r && g == other.g && b == other.b;
  35. }
  36. };
  37. } // namespace lumacs