ColorSwatch.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is Vision.
  13. *
  14. * The Initial Developer of the Original Code is The Vision Team.
  15. * Portions created by The Vision Team are
  16. * Copyright (C) 1999, 2000, 2001 The Vision Team. All Rights
  17. * Reserved.
  18. *
  19. * Contributor(s): Wade Majors <wade@ezri.org>
  20. * Rene Gollent
  21. */
  22. #define SHIFT_COMPONENT(x,y) (y >= 1 ? \
  23. ((uchar)(x * (2 - y))) : \
  24. ((uchar)(255 - y * (255 - x))))
  25. #include <Window.h>
  26. #include "ColorSwatch.h"
  27. struct general_ui_info
  28. {
  29. rgb_color background_color;
  30. rgb_color mark_color;
  31. rgb_color ighlight_color;
  32. bool color_frame;
  33. rgb_color window_frame_color;
  34. };
  35. extern general_ui_info general_info;
  36. ColorSwatch::ColorSwatch (
  37. BRect frame,
  38. const char *name,
  39. rgb_color color_,
  40. uint32 resize,
  41. uint32 flags)
  42. : BView (frame, name, resize, flags),
  43. fColor (color_)
  44. {
  45. }
  46. ColorSwatch::~ColorSwatch (void)
  47. {
  48. }
  49. void
  50. ColorSwatch::AttachedToWindow (void)
  51. {
  52. BView::AttachedToWindow();
  53. BView *view;
  54. if ((view = Parent()) != 0)
  55. {
  56. rgb_color c (view->ViewColor());
  57. SetViewColor (c);
  58. SetLowColor (c);
  59. }
  60. }
  61. void
  62. ColorSwatch::Draw (BRect)
  63. {
  64. PushState();
  65. SetDrawingMode (B_OP_COPY);
  66. rgb_color high (HighColor());
  67. BRect colorPad (Bounds());
  68. SetHighColor (ValueAsColor());
  69. FillRect (colorPad);
  70. rgb_color light (ShiftColor (ValueAsColor(), 0.4));
  71. rgb_color dark (ShiftColor (ValueAsColor(), 1.2));
  72. BeginLineArray (10);
  73. AddLine (
  74. colorPad.LeftTop(),
  75. colorPad.RightTop() - BPoint (1, 0),
  76. light);
  77. AddLine (
  78. colorPad.LeftTop(),
  79. colorPad.LeftBottom() - BPoint (0, 1),
  80. light);
  81. AddLine (
  82. colorPad.RightTop() + BPoint (0, 1),
  83. colorPad.RightBottom(),
  84. dark);
  85. AddLine (
  86. colorPad.RightBottom(),
  87. colorPad.LeftBottom() + BPoint (1, 0),
  88. dark);
  89. light = ShiftColor (ViewColor(), 0.1);
  90. dark = ShiftColor (ViewColor(), 1.4);
  91. colorPad.InsetBy (-1, -1);
  92. BPoint hless (-1, 0);
  93. BPoint hmore (1, 0);
  94. BPoint vless (0, -1);
  95. BPoint vmore (0, 1);
  96. if (IsFocus() && Window()->IsActive())
  97. {
  98. light = general_info.mark_color;
  99. dark = general_info.mark_color;
  100. hless = hmore = vless = vmore = BPoint (0, 0);
  101. }
  102. else
  103. {
  104. // A little blue residue clean up
  105. AddLine (
  106. colorPad.RightTop(),
  107. colorPad.RightTop(),
  108. ViewColor());
  109. AddLine (
  110. colorPad.LeftBottom(),
  111. colorPad.LeftBottom(),
  112. ViewColor());
  113. }
  114. AddLine (
  115. colorPad.LeftTop(),
  116. colorPad.RightTop() + hless,
  117. dark);
  118. AddLine (
  119. colorPad.LeftTop(),
  120. colorPad.LeftBottom() + vless,
  121. dark);
  122. AddLine (
  123. colorPad.RightTop() + vmore,
  124. colorPad.RightBottom(),
  125. light);
  126. AddLine (
  127. colorPad.RightBottom(),
  128. colorPad.LeftBottom() + hmore,
  129. light);
  130. EndLineArray();
  131. PopState();
  132. }
  133. rgb_color
  134. ColorSwatch::ValueAsColor (void) const
  135. {
  136. return fColor;
  137. }
  138. void
  139. ColorSwatch::SetColor (rgb_color c)
  140. {
  141. if (fColor.red == c.red
  142. && fColor.green == c.green
  143. && fColor.blue == c.blue
  144. && fColor.alpha == c.alpha)
  145. return;
  146. fColor = c;
  147. if (Parent()) Invalidate (Bounds());
  148. }
  149. rgb_color
  150. ColorSwatch::ShiftColor (rgb_color c, float percent) const
  151. {
  152. rgb_color result =
  153. {
  154. SHIFT_COMPONENT(c.red, percent),
  155. SHIFT_COMPONENT(c.green, percent),
  156. SHIFT_COMPONENT(c.blue, percent),
  157. SHIFT_COMPONENT(c.alpha, 255)
  158. };
  159. return result;
  160. }
  161. rgb_color
  162. ColorSwatch::Inverted (void) const
  163. {
  164. rgb_color baseColor, value = ValueAsColor();
  165. baseColor.red = 255 - value.red;
  166. baseColor.green = 255 - value.green;
  167. baseColor.blue = 255 - value.blue;
  168. baseColor.alpha = value.alpha;
  169. return fColor;
  170. }