color.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #ifndef __COLOR_H_
  3. #define __COLOR_H_
  4. #include <algorithm>
  5. #include "optype.h"
  6. #include <tchar.h>
  7. #define WORD_BKCOLOR 0
  8. #define WORD_COLOR 1
  9. #include <math.h>
  10. #define color2uint(color) (*(uint*)&color)
  11. template<typename T>
  12. constexpr T OP_ABS(T x) {
  13. return x < 0 ? -x : x;
  14. }
  15. template<typename T>
  16. constexpr bool IN_RANGE(T lhs, T rhs, T df) {
  17. return OP_ABS(lhs.b-rhs.b)<=df.b
  18. &&OP_ABS(lhs.g-rhs.g)<=df.g
  19. &&OP_ABS(lhs.r-rhs.r)<=df.r;
  20. }
  21. //#pragma pack(push)
  22. #pragma pack(1)
  23. struct color_t
  24. {
  25. //b is in low address ,alpha is in high address
  26. uchar b, g, r, alpha;
  27. color_t() :b(0), g(0), r(0), alpha(0) {}
  28. color_t(int b_, int g_, int r_) :b(b_), g(g_), r(r_),alpha(0xffu) {}
  29. color_t& str2color(const TString&s) {
  30. int r, g, b;
  31. TString ss = s;
  32. std::transform(ss.begin(), ss.end(), ss.begin(), ::toupper);
  33. int cnt = _stscanf(ss.c_str(), _T("%02X%02X%02X"), &r, &g, &b);
  34. this->b = b; this->r = r; this->g = g;
  35. return *this;
  36. }
  37. std::string tostr() {
  38. TCHAR buff[10];
  39. _stprintf(buff, _T("%02X%02X%02X"), r, g, b);
  40. return buff;
  41. }
  42. uchar toGray() const{
  43. return (r * 299 + g * 587 + b * 114 + 500) / 1000;
  44. }
  45. };
  46. #pragma pack()
  47. struct color_df_t {
  48. color_t color;
  49. color_t df;
  50. };
  51. struct pt_cr_df_t {
  52. int x, y;
  53. std::vector<color_df_t> crdfs;
  54. };
  55. #endif