DxLibEx
color.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2015-2017 DxLibEx project
3  https://github.com/Nagarei/DxLibEx/
4 
5  Distributed under the Boost Software License, Version 1.0.
6  (See http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #ifndef DXLE_INC_COLOR_HPP_
9 #define DXLE_INC_COLOR_HPP_
10 
12 #if defined(__c2__) && __clang_major__ == 3 && __clang_minor__ == 8
13 //To avoid compile error
14 //C:\Program Files (x86)\Windows Kits\8.1\Include\um\combaseapi.h(229,21): error : unknown type name 'IUnknown'
15 // static_cast<IUnknown*>(*pp); // make sure everyone derives from IUnknown
16 struct IUnknown;
17 #endif
18 #include "DxLib.h"
19 #include "dxlibex/config/defines.h"
21 #include <cstdint>
22 #include <cassert>
23 #include <type_traits>
24 
25 namespace dxle {
26 namespace color{
27 
28  enum class color_tag : unsigned {
29  black ,
30  blue ,
31  cyan ,
32  darkgray ,
33  gray ,
34  green ,
35  lightgray,
36  magenta ,
37  orange ,
38  pink ,
39  red ,
40  white ,
41  yellow ,
42 
43  //\~japanese color_tagの種類の数
45  };
46 
47  class color_base {};
48  template<typename T>
49  struct is_colortype : std::is_base_of<color_base, T>{};
50 
51  class rgb;
52  class dx_color;
53 
54  class rgb final : private color_base
55  {
56  private:
57  //like C#'s property
58  struct rgb_value_t {
59  DXLE_CONSTEXPR rgb_value_t()DXLE_NOEXCEPT_OR_NOTHROW :value() {}
60  DXLE_CONSTEXPR rgb_value_t(int v) : value(v)
61  {
62 #ifndef DXLE_NO_CXX14_CONSTEXPR
63  assert(0 <= v && v <= 255);
64 #endif
65  }
66 
67  DXLE_CONSTEXPR operator int()const DXLE_NOEXCEPT_OR_NOTHROW { return value; }
68  DXLE_CXX14_CONSTEXPR rgb_value_t& operator=(int v){ assert(0 <= v && v <= 255); value = v; return *this; }
69  DXLE_CXX14_CONSTEXPR rgb_value_t& operator=(const rgb_value_t& v)DXLE_NOEXCEPT_OR_NOTHROW { return (*this = static_cast<int>(v)); }
70  private:
71  int value;
72  };
73  public:
74  rgb_value_t red;
75  rgb_value_t green;
76  rgb_value_t blue;
77 
79  : red(), green(), blue()
80  {}
81  DXLE_CONSTEXPR rgb(int red_, int green_, int blue_)
82  : red(red_), green(green_), blue(blue_)
83  {}
86 
88  red = 255 - red; green = 255 - green; blue = 255 - blue;//理論上nothrow
89  return *this;
90  }
91  };
92 
93  class dx_color final : private color_base
94  {
95  public:
96  typedef decltype(DxLib::GetColor(0, 0, 0)) value_type;
97 
99  : value(0)
100  {}
101  dx_color(const dx_color&) DXLE_NOEXCEPT = default;
103  : value(static_cast<value_type&&>(other.value))
104  {}
105  dx_color(int Red, int Green, int Blue)
106  : value(DxLib::GetColor(Red, Green, Blue))
107  {
108 #ifndef DXLE_NO_CXX14_CONSTEXPR
109  assert(0 <= Red && Red <= 255);
110  assert(0 <= Green && Green <= 255);
111  assert(0 <= Blue && Blue <= 255);
112 #endif
113  }
115 
116  explicit dx_color(const rgb& rgb_color)DXLE_NOEXCEPT_OR_NOTHROW
117  : value(DxLib::GetColor(rgb_color.red, rgb_color.green, rgb_color.blue))
118  {}
119 
121  static dx_color MakeDxColor(int Red, int Green, int Blue) { return dx_color{ Red, Green, Blue }; }
123  static dx_color GetColor(int Red, int Green, int Blue) { return MakeDxColor(Red, Green, Blue); }
125  static dx_color GetColor3(const DxLib::COLORDATA *ColorData, int Red, int Green, int Blue, int Alpha = 255) { return DxLib::GetColor3(ColorData, Red, Green, Blue, Alpha); }
127  static dx_color GetColor4(const DxLib::COLORDATA *DestColorData, const DxLib::COLORDATA* SrcColorData, const dx_color& SrcColor) { return DxLib::GetColor4(DestColorData, SrcColorData, SrcColor.value); }
128 
129  //メンバ関数
130 
132  int GetColor2(int *Red, int *Green, int *Blue)const DXLE_NOEXCEPT_OR_NOTHROW { return GetRGB(Red, Green, Blue); }
134  int GetRGB(int *Red, int *Green, int *Blue)const DXLE_NOEXCEPT_OR_NOTHROW { return DxLib::GetColor2(value, Red, Green, Blue); }
136  rgb GetRGB()const DXLE_NOEXCEPT_OR_NOTHROW;
138  int GetColor5(const DxLib::COLORDATA *ColorData, int *Red, int *Green, int *Blue, int *Alpha = NULL)const DXLE_NOEXCEPT_OR_NOTHROW { return DxLib::GetColor5(ColorData, value, Red, Green, Blue, Alpha); }
139 
140  value_type get()const DXLE_NOEXCEPT_OR_NOTHROW { return value; }
141 
142  /*
143  extern COLOR_F GetColorF(float Red, float Green, float Blue, float Alpha); // 浮動小数点型のカラー値を作成する
144  extern COLOR_U8 GetColorU8(int Red, int Green, int Blue, int Alpha); // 符号なし整数8ビットのカラー値を作成する
145  extern int CreatePaletteColorData(DxLib::COLORDATA *ColorDataBuf); // パレットカラーのカラーフォーマットを構築する
146  extern int CreateARGBF32ColorData(DxLib::COLORDATA *ColorDataBuf); // ARGB各チャンネル 32bit 浮動小数点型カラーのカラーフォーマットを構築する
147  extern int CreateARGBF16ColorData(DxLib::COLORDATA *ColorDataBuf); // ARGB各チャンネル 16bit 浮動小数点型カラーのカラーフォーマットを構築する
148  extern int CreateXRGB8ColorData(DxLib::COLORDATA *ColorDataBuf); // XRGB8カラーのカラーフォーマットを構築する
149  extern int CreateARGB8ColorData(DxLib::COLORDATA *ColorDataBuf); // ARGB8カラーのカラーフォーマットを構築する
150  extern int CreateARGB4ColorData(DxLib::COLORDATA *ColorDataBuf); // ARGB4カラーのカラーフォーマットを構築する
151  extern int CreateFullColorData(DxLib::COLORDATA *ColorDataBuf); // 24ビットカラーのカラーフォーマットを構築する
152  extern int CreateGrayColorData(DxLib::COLORDATA *ColorDataBuf); // グレースケールのカラーフォーマットを構築する
153  extern int CreatePal8ColorData(DxLib::COLORDATA *ColorDataBuf); // パレット256色のカラーフォーマットを構築する
154  extern int CreateColorData(DxLib::COLORDATA *ColorDataBuf, int ColorBitDepth,
155  DWORD RedMask, DWORD GreenMask, DWORD BlueMask, DWORD AlphaMask,
156  int ChannelNum = 0, int ChannelBitDepth = 0, int FloatTypeFlag = FALSE); // カラーフォーマットを作成する
157  extern void SetColorDataNoneMask(DxLib::COLORDATA *ColorData); // NoneMask 以外の要素を埋めた DxLib::COLORDATA 構造体の情報を元に NoneMask をセットする
158  extern int CmpColorData(const DxLib::COLORDATA *ColorData1, const DxLib::COLORDATA *ColorData2); // 二つのカラーフォーマットが等しいかどうか調べる( 戻り値 TRUE:等しい FALSE:等しくない )
159  */
160 
161  private:
162  value_type value;
163  dx_color(value_type param)DXLE_NOEXCEPT_OR_NOTHROW
164  : value(param)
165  {}
166  };
167 
177  class dx_color_param final
178  {
179  public:
181  : value(other.value)
182  {}
183  template<typename color_t, typename enable_if<std::is_constructible<dx_color, color_t&&>::value, std::nullptr_t>::type = nullptr>
184  dx_color_param(color_t&& color) DXLE_NOEXCEPT_IF((std::is_nothrow_constructible<dx_color, color_t&&>::value))
185  : value(std::forward<color_t>(color))
186  {}
187  dx_color::value_type get()const DXLE_NOEXCEPT_OR_NOTHROW { return value.get(); }
188  private:
189  dx_color value;
190  };
191 
192  template<typename to, typename from, typename enable_if<is_colortype<from>::value && is_colortype<to>::value, nullptr_t>::type = nullptr>
193  to color_cast(from&& bace)
194  {
195  return static_cast<to>(std::forward<from>(bace));
196  }
197 
198 
199  //定義
200 
201 
202 
203  //rgb
204 
205 #if defined(DXLE_NO_CXX14_CONSTEXPR) && !defined(DXLE_NO_CXX11_CONSTEXPR)
206  inline DXLE_CONSTEXPR rgb::rgb(color_tag param)
207  : red(
208  (param == color_tag::black ) ? ( 0) :
209  (param == color_tag::blue ) ? ( 0) :
210  (param == color_tag::cyan ) ? ( 0) :
211  (param == color_tag::darkgray ) ? (169) :
212  (param == color_tag::gray ) ? (190) :
213  (param == color_tag::green ) ? ( 0) :
214  (param == color_tag::lightgray) ? (211) :
215  (param == color_tag::magenta ) ? (255) :
216  (param == color_tag::orange ) ? (255) :
217  (param == color_tag::pink ) ? (255) :
218  (param == color_tag::red ) ? (255) :
219  (param == color_tag::white ) ? (255) :
220  (param == color_tag::yellow ) ? (255) :
221  -1//error
222  )
223  , green(
224  (param == color_tag::black ) ? ( 0) :
225  (param == color_tag::blue ) ? ( 0) :
226  (param == color_tag::cyan ) ? (255) :
227  (param == color_tag::darkgray ) ? (169) :
228  (param == color_tag::gray ) ? (190) :
229  (param == color_tag::green ) ? (255) :
230  (param == color_tag::lightgray) ? (211) :
231  (param == color_tag::magenta ) ? ( 0) :
232  (param == color_tag::orange ) ? (165) :
233  (param == color_tag::pink ) ? (192) :
234  (param == color_tag::red ) ? ( 0) :
235  (param == color_tag::white ) ? (255) :
236  (param == color_tag::yellow ) ? (255) :
237  -1//error
238  )
239  , blue(
240  (param == color_tag::black ) ? ( 0) :
241  (param == color_tag::blue ) ? (255) :
242  (param == color_tag::cyan ) ? (255) :
243  (param == color_tag::darkgray ) ? (169) :
244  (param == color_tag::gray ) ? (190) :
245  (param == color_tag::green ) ? ( 0) :
246  (param == color_tag::lightgray) ? (211) :
247  (param == color_tag::magenta ) ? (255) :
248  (param == color_tag::orange ) ? ( 0) :
249  (param == color_tag::pink ) ? (203) :
250  (param == color_tag::red ) ? ( 0) :
251  (param == color_tag::white ) ? (255) :
252  (param == color_tag::yellow ) ? ( 0) :
253  -1//error
254  )
255  {
256  static_assert((unsigned)color_tag::numof_variation == 13u, "color variation is not match. Please debug DxLibEx.");
257  }
258 #else
260  {
261  DXLE_CONSTEXPR_OR_CONST int rgb_meta[][3] = {
262  { 0, 0, 0 }, //黒
263  { 0, 0, 255 }, //青
264  { 0, 255, 255 }, //シアン
265  { 169, 169, 169 }, //ダークグレー(X11)
266  { 190, 190, 190 }, //グレー
267  { 0, 255, 0 }, //緑(X11)
268  { 211, 211, 211 }, //ライトグレー
269  { 255, 0, 255 }, //マゼンタ
270  { 255, 165, 0 }, //オレンジ
271  { 255, 192, 203 }, //ピンク
272  { 255, 0, 0 }, //赤
273  { 255, 255, 255 }, //白
274  { 255, 255, 0 }, //黄色
275  };
276  static_assert((unsigned)color_tag::numof_variation == (sizeof(rgb_meta) / sizeof(rgb_meta[0])), "color variation is not match. Please debug DxLibEx.");
277  assert(0 <= (unsigned)color_tag_ && (unsigned)color_tag_ < (unsigned)color_tag::numof_variation);
278 
279  red = rgb_meta[(unsigned)color_tag_][0];
280  green = rgb_meta[(unsigned)color_tag_][1];
281  blue = rgb_meta[(unsigned)color_tag_][2];
282  }
283 #endif
285  : rgb(dx_color_.GetRGB())
286  {}
287 
288  //dx_color
289 
290  inline dx_color::dx_color(color_tag color_tag_)
291  : value(static_cast<dx_color>(rgb{ color_tag_ }).get())
292  {}
295  {
296  int r, g, b;
297  dx_color::GetColor2(&r, &g, &b);
298  return rgb{ r, g, b };
299  }
300 
301 
302 }//namespace
303 using namespace color;
304 }//namespace
305 
306 
307 #if 0
309 #include "DxLib.h"
310 #include "config/defines.h"
311 
312 namespace dxle
313 {
316  struct Color
317  {
318  typedef decltype(DxLib::GetColor(0, 0, 0)) value_type;
319 
320  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor( 0, 0, 0); } } static const black ;
321  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor( 0, 0, 255); } } static const blue ;
322  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor( 0, 255, 255); } } static const cyan ;
323  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(169, 169, 169); } } static const darkgray ;
324  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(190, 190, 190); } } static const gray ;
325  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor( 0, 255, 0); } } static const green ;
326  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(211, 211, 211); } } static const lightgray;
327  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(255, 0, 255); } } static const magenta ;
328  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(255, 165, 0); } } static const orange ;
329  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(255, 192, 203); } } static const pink ;
330  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(255, 0, 0); } } static const red ;
331  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(255, 255, 255); } } static const white ;
332  struct { inline operator Color()const DXLE_NOEXCEPT { return DxLib::GetColor(255, 255, 0); } } static const yellow ;
333 
335  static Color GetColor(int Red, int Green, int Blue)DXLE_NOEXCEPT { return DxLib::GetColor(Red, Green, Blue); }
337  static Color GetColor3(const COLORDATA *ColorData, int Red, int Green, int Blue, int Alpha = 255)DXLE_NOEXCEPT { return DxLib::GetColor3(ColorData, Red, Green, Blue, Alpha); }
339  static Color GetColor4(const COLORDATA *DestColorData, const COLORDATA* SrcColorData, const Color& SrcColor)DXLE_NOEXCEPT { return DxLib::GetColor4(DestColorData, SrcColorData, SrcColor.value); }
340 
341  //メンバ関数
342 
344  int GetColor2(int *Red, int *Green, int *Blue)const DXLE_NOEXCEPT { return DxLib::GetColor2(value, Red, Green, Blue); }
346  int GetColor5(const COLORDATA *ColorData, int *Red, int *Green, int *Blue, int *Alpha = NULL)const DXLE_NOEXCEPT { return DxLib::GetColor5(ColorData, value, Red, Green, Blue, Alpha); }
347 
348  value_type get()const DXLE_NOEXCEPT { return value; }
349 
350  /*
351  extern COLOR_F GetColorF(float Red, float Green, float Blue, float Alpha); // 浮動小数点型のカラー値を作成する
352  extern COLOR_U8 GetColorU8(int Red, int Green, int Blue, int Alpha); // 符号なし整数8ビットのカラー値を作成する
353  extern int CreatePaletteColorData(COLORDATA *ColorDataBuf); // パレットカラーのカラーフォーマットを構築する
354  extern int CreateARGBF32ColorData(COLORDATA *ColorDataBuf); // ARGB各チャンネル 32bit 浮動小数点型カラーのカラーフォーマットを構築する
355  extern int CreateARGBF16ColorData(COLORDATA *ColorDataBuf); // ARGB各チャンネル 16bit 浮動小数点型カラーのカラーフォーマットを構築する
356  extern int CreateXRGB8ColorData(COLORDATA *ColorDataBuf); // XRGB8カラーのカラーフォーマットを構築する
357  extern int CreateARGB8ColorData(COLORDATA *ColorDataBuf); // ARGB8カラーのカラーフォーマットを構築する
358  extern int CreateARGB4ColorData(COLORDATA *ColorDataBuf); // ARGB4カラーのカラーフォーマットを構築する
359  extern int CreateFullColorData(COLORDATA *ColorDataBuf); // 24ビットカラーのカラーフォーマットを構築する
360  extern int CreateGrayColorData(COLORDATA *ColorDataBuf); // グレースケールのカラーフォーマットを構築する
361  extern int CreatePal8ColorData(COLORDATA *ColorDataBuf); // パレット256色のカラーフォーマットを構築する
362  extern int CreateColorData(COLORDATA *ColorDataBuf, int ColorBitDepth,
363  DWORD RedMask, DWORD GreenMask, DWORD BlueMask, DWORD AlphaMask,
364  int ChannelNum = 0, int ChannelBitDepth = 0, int FloatTypeFlag = FALSE); // カラーフォーマットを作成する
365  extern void SetColorDataNoneMask(COLORDATA *ColorData); // NoneMask 以外の要素を埋めた COLORDATA 構造体の情報を元に NoneMask をセットする
366  extern int CmpColorData(const COLORDATA *ColorData1, const COLORDATA *ColorData2); // 二つのカラーフォーマットが等しいかどうか調べる( 戻り値 TRUE:等しい FALSE:等しくない )
367  */
368 
369  private:
370  value_type value;
371  Color(value_type param)DXLE_NOEXCEPT
372  : value(param)
373  {}
374  };
375 
376 }//namespace dxle
377 #endif//#if 0 old version
378 
379 
380 #endif
rgb GetRGB() const DXLE_NOEXCEPT_OR_NOTHROW
カラー値から赤、緑、青の値を取得する
Definition: color.hpp:294
#define DXLE_CONSTEXPR
Definition: suffix.hpp:21
static dx_color MakeDxColor(int Red, int Green, int Blue)
DrawPixel 等の描画関数で使用するカラー値を取得する
Definition: color.hpp:121
int GetColor5(const DxLib::COLORDATA *ColorData, int *Red, int *Green, int *Blue, int *Alpha=NULL) const DXLE_NOEXCEPT_OR_NOTHROW
指定のカラーフォーマットのカラー値を赤、緑、青、アルファの値を取得する
Definition: color.hpp:138
decltype(DxLib::GetColor(0, 0, 0)) typedef value_type
Definition: color.hpp:96
DXLE_CXX14_CONSTEXPR rgb & reverse() DXLE_NOEXCEPT_OR_NOTHROW
Definition: color.hpp:87
dx_color(const rgb &rgb_color) DXLE_NOEXCEPT_OR_NOTHROW
Definition: color.hpp:116
#define DXLE_NOEXCEPT
Definition: suffix.hpp:93
rgb_value_t green
Definition: color.hpp:75
rgb_value_t red
Definition: color.hpp:74
static dx_color GetColor(int Red, int Green, int Blue)
DrawPixel 等の描画関数で使用するカラー値を取得する
Definition: color.hpp:123
Definition: point2d.hpp:672
Definition: cast_if.hpp:12
#define DXLE_CONSTEXPR_OR_CONST
Definition: suffix.hpp:22
#define DXLE_CXX14_CONSTEXPR
Definition: suffix.hpp:42
to color_cast(from &&bace)
Definition: color.hpp:193
DXLE_CONSTEXPR rgb(int red_, int green_, int blue_)
Definition: color.hpp:81
DXLE_CONSTEXPR dx_color(dx_color &&other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: color.hpp:102
int GetRGB(int *Red, int *Green, int *Blue) const DXLE_NOEXCEPT_OR_NOTHROW
カラー値から赤、緑、青の値を取得する
Definition: color.hpp:134
dx_color_param(const dx_color_param &other) DXLE_NOEXCEPT_OR_NOTHROW
Definition: color.hpp:180
dx_color(int Red, int Green, int Blue)
Definition: color.hpp:105
#define DXLE_NOEXCEPT_IF(COND)
Definition: suffix.hpp:107
DXLE_CONSTEXPR dx_color() DXLE_NOEXCEPT_OR_NOTHROW
Definition: color.hpp:98
rgb_value_t blue
Definition: color.hpp:76
#define DXLE_NOEXCEPT_OR_NOTHROW
Definition: suffix.hpp:94
ダークグレー(X11)
int GetColor2(int *Red, int *Green, int *Blue) const DXLE_NOEXCEPT_OR_NOTHROW
カラー値から赤、緑、青の値を取得する
Definition: color.hpp:132
dx_color_param(color_t &&color) DXLE_NOEXCEPT_IF((std
Definition: color.hpp:184
DXLE_CONSTEXPR rgb() DXLE_NOEXCEPT_OR_NOTHROW
Definition: color.hpp:78
static dx_color GetColor4(const DxLib::COLORDATA *DestColorData, const DxLib::COLORDATA *SrcColorData, const dx_color &SrcColor)
指定のカラーフォーマットのカラー値を別のカラーフォーマットのカラー値に変換する
Definition: color.hpp:127
static dx_color GetColor3(const DxLib::COLORDATA *ColorData, int Red, int Green, int Blue, int Alpha=255)
指定のピクセルフォーマットに対応したカラー値を得る
Definition: color.hpp:125