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.

190 lines
4.9 KiB

18 years ago
18 years ago
13 years ago
18 years ago
13 years ago
13 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /************/
  2. /* colors.h */
  3. /************/
  4. #ifndef _COLORS_H
  5. #define _COLORS_H
  6. #include <wx/wx.h>
  7. /** The color enumeration. Also contains a flag and the alpha value in
  8. * the upper bits
  9. */
  10. enum EDA_COLOR_T
  11. {
  12. UNSPECIFIED_COLOR = -1,
  13. BLACK = 0,
  14. DARKDARKGRAY,
  15. DARKGRAY,
  16. LIGHTGRAY,
  17. WHITE,
  18. LIGHTYELLOW,
  19. DARKBLUE,
  20. DARKGREEN,
  21. DARKCYAN,
  22. DARKRED,
  23. DARKMAGENTA,
  24. DARKBROWN,
  25. BLUE,
  26. GREEN,
  27. CYAN,
  28. RED,
  29. MAGENTA,
  30. BROWN,
  31. LIGHTBLUE,
  32. LIGHTGREEN,
  33. LIGHTCYAN,
  34. LIGHTRED,
  35. LIGHTMAGENTA,
  36. YELLOW,
  37. PUREBLUE,
  38. PUREGREEN,
  39. PURECYAN,
  40. PURERED,
  41. PUREMAGENTA,
  42. PUREYELLOW,
  43. NBCOLORS, ///< Number of colors
  44. HIGHLIGHT_FLAG = ( 1<<19 ),
  45. MASKCOLOR = 31 ///< mask for color index into g_ColorRefs[]
  46. };
  47. /// Checked cast. Use only when necessary (usually I/O)
  48. inline EDA_COLOR_T ColorFromInt( int aColor )
  49. {
  50. wxASSERT( aColor >= UNSPECIFIED_COLOR && aColor < NBCOLORS );
  51. return static_cast<EDA_COLOR_T>( aColor );
  52. }
  53. inline EDA_COLOR_T NextColor( EDA_COLOR_T& aColor )
  54. {
  55. // We have to accept NBCOLORS for loop termination conditions
  56. wxASSERT( aColor >= UNSPECIFIED_COLOR && aColor <= NBCOLORS );
  57. aColor = static_cast<EDA_COLOR_T>( int( aColor ) + 1 );
  58. return aColor;
  59. }
  60. /// Return only the plain color part
  61. inline EDA_COLOR_T ColorGetBase( EDA_COLOR_T aColor)
  62. {
  63. EDA_COLOR_T base = static_cast<EDA_COLOR_T>( aColor & MASKCOLOR );
  64. return base;
  65. }
  66. /// Mix two colors in some way (hopefully like a logical OR)
  67. EDA_COLOR_T ColorMix( EDA_COLOR_T aColor1, EDA_COLOR_T aColor2 );
  68. /// Force the color part of a color to darkdarkgray
  69. inline void ColorTurnToDarkDarkGray( EDA_COLOR_T *aColor )
  70. {
  71. *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) & ~MASKCOLOR) | DARKDARKGRAY );
  72. }
  73. inline void ColorChangeHighlightFlag( EDA_COLOR_T *aColor, bool flag )
  74. {
  75. if( flag )
  76. *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) | HIGHLIGHT_FLAG ) );
  77. else
  78. *aColor = static_cast<EDA_COLOR_T>( (int(*aColor) & ~HIGHLIGHT_FLAG ) );
  79. }
  80. /**
  81. * Function SetAlpha
  82. * ORs in the alpha blend parameter in to a color index.
  83. */
  84. inline void SetAlpha( EDA_COLOR_T* aColor, unsigned char aBlend )
  85. {
  86. const unsigned char MASKALPHA = 0xFF;
  87. *aColor = static_cast<EDA_COLOR_T>((*aColor & ~(MASKALPHA << 24))
  88. | ((aBlend & MASKALPHA) << 24));
  89. }
  90. /**
  91. * Function GetAlpha
  92. * returns the alpha blend parameter from a color index.
  93. */
  94. inline unsigned char GetAlpha( EDA_COLOR_T aColor )
  95. {
  96. const unsigned char MASKALPHA = 0xFF;
  97. return (aColor >> 24) & MASKALPHA;
  98. }
  99. struct StructColors
  100. {
  101. unsigned char m_Blue;
  102. unsigned char m_Green;
  103. unsigned char m_Red;
  104. EDA_COLOR_T m_Numcolor;
  105. const wxChar* m_Name;
  106. EDA_COLOR_T m_LightColor;
  107. };
  108. /// list of existing Colors
  109. extern const StructColors g_ColorRefs[NBCOLORS];
  110. /// Step a color to the highlighted version if the highlight flag is set
  111. inline void ColorApplyHighlightFlag( EDA_COLOR_T *aColor )
  112. {
  113. EDA_COLOR_T base = ColorGetBase( *aColor );
  114. wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
  115. if( *aColor & HIGHLIGHT_FLAG )
  116. *aColor = g_ColorRefs[base].m_LightColor;
  117. }
  118. /// Find a color by name
  119. EDA_COLOR_T ColorByName( const wxChar *aName );
  120. /// Find the nearest color match
  121. EDA_COLOR_T ColorFindNearest( const wxColour &aColor );
  122. /**
  123. * Check if a color is light i.e. if black would be more readable than
  124. * white on it
  125. */
  126. bool ColorIsLight( EDA_COLOR_T aColor );
  127. inline const wxChar *ColorGetName( EDA_COLOR_T aColor )
  128. {
  129. EDA_COLOR_T base = ColorGetBase( aColor );
  130. wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
  131. return g_ColorRefs[base].m_Name;
  132. }
  133. inline void ColorSetBrush( wxBrush *aBrush, EDA_COLOR_T aColor )
  134. {
  135. EDA_COLOR_T base = ColorGetBase( aColor );
  136. wxASSERT( base > UNSPECIFIED_COLOR && base < NBCOLORS );
  137. const StructColors &col = g_ColorRefs[base];
  138. aBrush->SetColour( col.m_Red, col.m_Green, col.m_Blue );
  139. }
  140. /**
  141. * Function MakeColour
  142. * returns a wxWidgets wxColor from a KiCad color index with alpha value.
  143. * Note that alpha support is not available on every wxWidgets platform. On
  144. * such platform the behavior is the same as for wxALPHA_OPAQUE and that
  145. * means the alpha value has no effect and will be ignored. wxGtk 2.8.4 is
  146. * not supporting alpha.
  147. * @return wxColour - given a KiCad color index with alpha value
  148. */
  149. inline wxColour MakeColour( EDA_COLOR_T aColor )
  150. {
  151. #if wxCHECK_VERSION(2,8,5)
  152. int alpha = GetAlpha( aColor );
  153. alpha = alpha ? alpha : wxALPHA_OPAQUE;
  154. #endif
  155. EDA_COLOR_T ndx = ColorGetBase( aColor );
  156. wxASSERT( ndx > UNSPECIFIED_COLOR && ndx < NBCOLORS );
  157. return wxColour( g_ColorRefs[ndx].m_Red,
  158. g_ColorRefs[ndx].m_Green,
  159. g_ColorRefs[ndx].m_Blue
  160. #if wxCHECK_VERSION(2,8,5)
  161. ,(unsigned char) alpha
  162. #endif
  163. );
  164. }
  165. #endif /* ifndef _COLORS_H */