#pragma once
#ifndef __COLOR_H_
#define __COLOR_H_
#include <algorithm>
#include "optype.h"
#include <tchar.h>
#define WORD_BKCOLOR 0
#define WORD_COLOR 1
#include <math.h>

#define color2uint(color) (*(uint*)&color)

template<typename T>
constexpr T OP_ABS(T x) {
	return x < 0 ? -x : x;
}

template<typename T>
constexpr bool IN_RANGE(T lhs, T rhs, T df) { 
	return OP_ABS(lhs.b-rhs.b)<=df.b
		&&OP_ABS(lhs.g-rhs.g)<=df.g
		&&OP_ABS(lhs.r-rhs.r)<=df.r;
}

//#pragma pack(push)
#pragma pack(1)
struct color_t 
{
	//b is in low address ,alpha is in high address
	uchar b, g, r, alpha;
	color_t() :b(0), g(0), r(0), alpha(0) {}
	color_t(int b_, int g_, int r_) :b(b_), g(g_), r(r_),alpha(0xffu) {}
	
	color_t& str2color(const TString&s) {
		int r, g, b;
		TString ss = s;
		std::transform(ss.begin(), ss.end(), ss.begin(), ::toupper);
		int cnt = _stscanf(ss.c_str(), _T("%02X%02X%02X"), &r, &g, &b);
		this->b = b; this->r = r; this->g = g;
		return *this;
	}

	std::string tostr() {
		TCHAR buff[10];
		_stprintf(buff, _T("%02X%02X%02X"), r, g, b);
		return buff;
	}

	uchar toGray() const{
		return (r * 299 + g * 587 + b * 114 + 500) / 1000;
	}
};

#pragma pack()

struct color_df_t {
	color_t color;
	color_t df;
};

struct pt_cr_df_t {
	int x, y;
	std::vector<color_df_t> crdfs;
};

#endif
