You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
12 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
  5. * Copyright (C) 2017-2023 Kicad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * Color class
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #ifndef COLOR4D_H_
  27. #define COLOR4D_H_
  28. #include <wx/debug.h>
  29. #include <wx/colour.h>
  30. #include <wx/string.h>
  31. #include <nlohmann/json_fwd.hpp>
  32. /**
  33. * Legacy color enumeration. Also contains a flag and the alpha value in the upper bits
  34. */
  35. enum EDA_COLOR_T
  36. {
  37. UNSPECIFIED_COLOR = -1,
  38. BLACK = 0,
  39. DARKDARKGRAY,
  40. DARKGRAY,
  41. LIGHTGRAY,
  42. WHITE,
  43. LIGHTYELLOW,
  44. DARKBLUE,
  45. DARKGREEN,
  46. DARKCYAN,
  47. DARKRED,
  48. DARKMAGENTA,
  49. DARKBROWN,
  50. BLUE,
  51. GREEN,
  52. CYAN,
  53. RED,
  54. MAGENTA,
  55. BROWN,
  56. LIGHTBLUE,
  57. LIGHTGREEN,
  58. LIGHTCYAN,
  59. LIGHTRED,
  60. LIGHTMAGENTA,
  61. YELLOW,
  62. PUREBLUE,
  63. PUREGREEN,
  64. PURECYAN,
  65. PURERED,
  66. PUREMAGENTA,
  67. PUREYELLOW,
  68. LIGHTERORANGE,
  69. DARKORANGE,
  70. ORANGE,
  71. LIGHTORANGE,
  72. PUREORANGE,
  73. NBCOLORS, ///< Number of colors
  74. HIGHLIGHT_FLAG = ( 1<<19 ),
  75. MASKCOLOR = 31 ///< mask for color index into colorRefs()[]
  76. };
  77. struct StructColors
  78. {
  79. unsigned char m_Blue;
  80. unsigned char m_Green;
  81. unsigned char m_Red;
  82. EDA_COLOR_T m_Numcolor;
  83. std::string m_ColorName;
  84. EDA_COLOR_T m_LightColor;
  85. };
  86. /// Global list of legacy color names, still used all over the place for constructing COLOR4D's
  87. const StructColors* colorRefs();
  88. namespace KIGFX
  89. {
  90. /**
  91. * A color representation with 4 components: red, green, blue, alpha.
  92. */
  93. class COLOR4D
  94. {
  95. public:
  96. // Constructor (creates the Color 0,0,0,0)
  97. COLOR4D() :
  98. r( 0 ),
  99. g( 0 ),
  100. b( 0 ),
  101. a( 1.0 )
  102. {
  103. }
  104. /**
  105. * @param aRed is the red component [0.0 .. 1.0].
  106. * @param aGreen is the green component [0.0 .. 1.0].
  107. * @param aBlue is the blue component [0.0 .. 1.0].
  108. * @param aAlpha is the alpha value [0.0 .. 1.0].
  109. */
  110. COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
  111. r( aRed ),
  112. g( aGreen ),
  113. b( aBlue ),
  114. a( aAlpha )
  115. {
  116. wxASSERT( r >= 0.0 && r <= 1.0 );
  117. wxASSERT( g >= 0.0 && g <= 1.0 );
  118. wxASSERT( b >= 0.0 && b <= 1.0 );
  119. wxASSERT( a >= 0.0 && a <= 1.0 );
  120. }
  121. /**
  122. * @param aColor is one of KiCad's palette colors.
  123. * @see EDA_COLOR_T
  124. */
  125. COLOR4D( EDA_COLOR_T aColor );
  126. /**
  127. * Initialize the color from a RGBA value with 0-255 red/green/blue and 0-1 alpha.
  128. *
  129. * Suitable for taking the values directly from the "CSS syntax" from ToWxString.
  130. *
  131. * @return this color.
  132. */
  133. COLOR4D& FromCSSRGBA( int aRed, int aGreen, int aBlue, double aAlpha = 1.0 );
  134. /**
  135. * Defines a color from a CSS or HTML-type string
  136. * @param aColorStr input string
  137. */
  138. COLOR4D( const wxString& aColorStr );
  139. /**
  140. * @param aColor is the color type used by wxWidgets.
  141. */
  142. COLOR4D( const wxColour& aColor );
  143. /**
  144. * Set color values by parsing a string using wxColour::Set().
  145. *
  146. * @param aColorString is a color string that wxColour can understand.
  147. * @return true if color was set successfully.
  148. */
  149. bool SetFromWxString( const wxString& aColorString );
  150. wxString ToCSSString() const;
  151. bool SetFromHexString( const wxString& aColorString );
  152. wxString ToHexString() const;
  153. wxColour ToColour() const;
  154. /**
  155. * Mix this COLOR4D with an input COLOR4D using the OR-mixing of legacy canvas.
  156. *
  157. * Can be removed once legacy canvas is removed. Depends on wxColour for simplicity,
  158. * but could be re-written to avoid this dependency if desired.
  159. *
  160. * @param aColor The color to mix with this one
  161. */
  162. COLOR4D LegacyMix( const COLOR4D& aColor ) const;
  163. /**
  164. * Converts current color (stored in RGB) to HSL format.
  165. *
  166. * @param aOutHue is the conversion result for hue component, in degrees 0 ... 360.0.
  167. * @param aOutSaturation is the conversion result for saturation component (0 ... 1.0).
  168. * @param aOutLightness is conversion result for value component (0 ... 1.0).
  169. * @note saturation is set to 0.0 for black color if r = g = b,
  170. */
  171. void ToHSL( double& aOutHue, double& aOutSaturation, double& aOutValue ) const;
  172. /**
  173. * Change currently used color to the one given by hue, saturation and lightness parameters.
  174. *
  175. * @param aInHue is hue component, in degrees (0.0 - 360.0).
  176. * @param aInSaturation is saturation component (0.0 - 1.0).
  177. * @param aInLightness is lightness component (0.0 - 1.0).
  178. */
  179. void FromHSL( double aInHue, double aInSaturation, double aInLightness );
  180. /**
  181. * Makes the color brighter by a given factor.
  182. *
  183. * @param aFactor Specifies how bright the color should become (valid values: 0.0 .. 1.0).
  184. * @return COLOR4D& Brightened color.
  185. */
  186. COLOR4D& Brighten( double aFactor )
  187. {
  188. wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
  189. r = r * ( 1.0 - aFactor ) + aFactor;
  190. g = g * ( 1.0 - aFactor ) + aFactor;
  191. b = b * ( 1.0 - aFactor ) + aFactor;
  192. return *this;
  193. }
  194. /**
  195. * Makes the color darker by a given factor.
  196. *
  197. * @param aFactor Specifies how dark the color should become (valid values: 0.0 .. 1.0).
  198. * @return COLOR4D& Darkened color.
  199. */
  200. COLOR4D& Darken( double aFactor )
  201. {
  202. wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
  203. r = r * ( 1.0 - aFactor );
  204. g = g * ( 1.0 - aFactor );
  205. b = b * ( 1.0 - aFactor );
  206. return *this;
  207. }
  208. /**
  209. * Makes the color inverted, alpha remains the same.
  210. *
  211. * @return COLOR4D& Inverted color.
  212. */
  213. COLOR4D& Invert()
  214. {
  215. r = ( 1.0 - r );
  216. g = ( 1.0 - g );
  217. b = ( 1.0 - b );
  218. return *this;
  219. }
  220. /**
  221. * Saturates the color to a given factor (in HSV model)
  222. */
  223. COLOR4D& Saturate( double aFactor );
  224. /**
  225. * Removes color (in HSL model)
  226. * @return greyscale version of color
  227. */
  228. COLOR4D& Desaturate();
  229. /**
  230. * Return a color that is brighter by a given factor, without modifying object.
  231. *
  232. * @param aFactor Specifies how bright the color should become (valid values: 0.0 .. 1.0).
  233. * @return COLOR4D Highlighted color.
  234. */
  235. COLOR4D Brightened( double aFactor ) const
  236. {
  237. wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
  238. return COLOR4D( r * ( 1.0 - aFactor ) + aFactor, g * ( 1.0 - aFactor ) + aFactor,
  239. b * ( 1.0 - aFactor ) + aFactor, a );
  240. }
  241. /**
  242. * Return a color that is darker by a given factor, without modifying object.
  243. *
  244. * @param aFactor Specifies how dark the color should become (valid values: 0.0 .. 1.0).
  245. * @return COLOR4D Darkened color.
  246. */
  247. COLOR4D Darkened( double aFactor ) const
  248. {
  249. wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
  250. return COLOR4D( r * ( 1.0 - aFactor ), g * ( 1.0 - aFactor ), b * ( 1.0 - aFactor ), a );
  251. }
  252. /**
  253. * Return a color that is mixed with the input by a factor.
  254. *
  255. * @param aFactor Specifies how much of the original color to keep (valid values: 0.0 .. 1.0).
  256. * @return COLOR4D Mixed color.
  257. */
  258. COLOR4D Mix( const COLOR4D& aColor, double aFactor ) const
  259. {
  260. wxASSERT( aFactor >= 0.0 && aFactor <= 1.0 );
  261. return COLOR4D( aColor.r * ( 1.0 - aFactor ) + r * aFactor,
  262. aColor.g * ( 1.0 - aFactor ) + g * aFactor,
  263. aColor.b * ( 1.0 - aFactor ) + b * aFactor,
  264. a );
  265. }
  266. /**
  267. * Return a color with the same color, but the given alpha.
  268. *
  269. * @param aAlpha specifies the alpha of the new color
  270. * @return COLOR4D color with that alpha
  271. */
  272. COLOR4D WithAlpha( double aAlpha ) const
  273. {
  274. wxASSERT( aAlpha >= 0.0 && aAlpha <= 1.0 );
  275. return COLOR4D( r, g, b, aAlpha );
  276. }
  277. /**
  278. * Returns an inverted color, alpha remains the same.
  279. *
  280. * @return COLOR4D& Inverted color.
  281. */
  282. COLOR4D Inverted() const
  283. {
  284. return COLOR4D( 1.0 - r, 1.0 - g, 1.0 - b, a );
  285. }
  286. /**
  287. * Returns the brightness value of the color ranged from 0.0 to 1.0.
  288. *
  289. * @return The brightness value.
  290. */
  291. double GetBrightness() const
  292. {
  293. // Weighted W3C formula
  294. return r * 0.299 + g * 0.587 + b * 0.117;
  295. }
  296. /**
  297. * Convert current color (stored in RGB) to HSV format.
  298. *
  299. * @param aOutHue is the conversion result for hue component, in degrees 0 ... 360.0.
  300. * @param aOutSaturation is the conversion result for saturation component (0 ... 1.0).
  301. * @param aOutValue is conversion result for value component (0 ... 1.0).
  302. * @param aAlwaysDefineHue controls the way hue is defined when r = v = b
  303. * @note saturation is set to 0.0 for black color (r = v = b = 0), and if r = v = b,
  304. * hue is set to 0.0 if aAlwaysDefineHue = true, and set to NAN if aAlwaysDefineHue = false.
  305. * this option is useful to convert a 4D color to a legacy color, because Red has hue = 0,
  306. * therefore aAlwaysDefineHue = false makes difference between Red and Gray colors.
  307. */
  308. void ToHSV( double& aOutHue, double& aOutSaturation, double& aOutValue,
  309. bool aAlwaysDefineHue = false ) const;
  310. /**
  311. * Changes currently used color to the one given by hue, saturation and value parameters.
  312. *
  313. * @param aInH is hue component, in degrees.
  314. * @param aInS is saturation component.
  315. * @param aInV is value component.
  316. */
  317. void FromHSV( double aInH, double aInS, double aInV );
  318. /**
  319. * Returns the distance (in RGB space) between two colors.
  320. */
  321. double Distance( const COLOR4D& other ) const;
  322. int Compare( const COLOR4D& aRhs ) const;
  323. /**
  324. * Returns a legacy color ID that is closest to the given 8-bit RGB values.
  325. */
  326. static EDA_COLOR_T FindNearestLegacyColor( int aR, int aG, int aB );
  327. // Color components: red, green, blue, alpha
  328. double r; ///< Red component
  329. double g; ///< Green component
  330. double b; ///< Blue component
  331. double a; ///< Alpha component
  332. /// For legacy support; used as a value to indicate color hasn't been set yet
  333. static const COLOR4D UNSPECIFIED;
  334. // Declare a few color shortcuts that are used for comparisons frequently
  335. static const COLOR4D WHITE;
  336. static const COLOR4D BLACK;
  337. static const COLOR4D CLEAR;
  338. };
  339. /// @brief Equality operator, are two colors equal
  340. const bool operator==( const COLOR4D& lhs, const COLOR4D& rhs );
  341. /// @brief Not equality operator, are two colors not equal
  342. const bool operator!=( const COLOR4D& lhs, const COLOR4D& rhs );
  343. const bool operator<( const COLOR4D& lhs, const COLOR4D& rhs );
  344. /// Syntactic sugar for outputting colors to strings
  345. std::ostream &operator<<( std::ostream &aStream, COLOR4D const &aColor );
  346. // to allow json( COLOR4D )
  347. void to_json( nlohmann::json& aJson, const COLOR4D& aColor );
  348. // To allow json::get<COLOR4D>()
  349. void from_json( const nlohmann::json& aJson, COLOR4D& aColor );
  350. } // namespace KIGFX
  351. #endif /* COLOR4D_H_ */