GR32_ColorSwatch.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. unit GR32_ColorSwatch;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2009
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Christan-W. Budde <[email protected]>
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$I GR32.inc}
  36. uses
  37. {$IFDEF FPC}
  38. LCLIntf, LCLType, LMessages, Types,
  39. {$IFDEF MSWINDOWS}
  40. Windows,
  41. {$ENDIF}
  42. {$ELSE}
  43. Windows, Messages,
  44. {$ENDIF}
  45. Classes, Controls, Forms, GR32, GR32_Containers;
  46. type
  47. TCustomColorSwatch = class(TCustomControl)
  48. private
  49. FBuffer: TBitmap32;
  50. FColor: TColor32;
  51. FBufferValid: Boolean;
  52. FBorder: Boolean;
  53. procedure SetBorder(const Value: Boolean);
  54. procedure SetColor(const Value: TColor32); reintroduce;
  55. {$IFDEF FPC}
  56. procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
  57. procedure WMGetDlgCode(var Msg: TLMessage); message LM_GETDLGCODE;
  58. {$ELSE}
  59. procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
  60. procedure WMGetDlgCode(var Msg: TWmGetDlgCode); message WM_GETDLGCODE;
  61. {$ENDIF}
  62. protected
  63. procedure Paint; override;
  64. public
  65. constructor Create(AOwner: TComponent); override;
  66. destructor Destroy; override;
  67. procedure Invalidate; override;
  68. procedure Resize; override;
  69. property Border: Boolean read FBorder write SetBorder default False;
  70. property Color: TColor32 read FColor write SetColor;
  71. end;
  72. TColorSwatch = class(TCustomColorSwatch)
  73. published
  74. property Align;
  75. property Anchors;
  76. property Border;
  77. property Color;
  78. property DragCursor;
  79. property DragKind;
  80. property Enabled;
  81. {$IFNDEF FPC}
  82. property ParentBackground;
  83. {$ENDIF}
  84. property ParentColor;
  85. property ParentShowHint;
  86. property PopupMenu;
  87. property TabOrder;
  88. property TabStop;
  89. {$IFNDEF PLATFORM_INDEPENDENT}
  90. property OnCanResize;
  91. {$ENDIF}
  92. property OnClick;
  93. property OnDblClick;
  94. property OnDragDrop;
  95. property OnDragOver;
  96. property OnEndDrag;
  97. property OnMouseDown;
  98. property OnMouseMove;
  99. property OnMouseUp;
  100. property OnMouseWheel;
  101. property OnMouseWheelDown;
  102. property OnMouseWheelUp;
  103. {$IFDEF COMPILER2005_UP}
  104. property OnMouseEnter;
  105. property OnMouseLeave;
  106. {$ENDIF}
  107. property OnResize;
  108. property OnStartDrag;
  109. end;
  110. implementation
  111. uses
  112. Math, Graphics, GR32_Backends, GR32_Math, GR32_Blend, GR32_VectorUtils;
  113. { TCustomColorSwatch }
  114. constructor TCustomColorSwatch.Create(AOwner: TComponent);
  115. begin
  116. inherited Create(AOwner);
  117. ControlStyle := ControlStyle + [csOpaque];
  118. FBuffer := TBitmap32.Create;
  119. FColor := clSalmon32;
  120. end;
  121. destructor TCustomColorSwatch.Destroy;
  122. begin
  123. FBuffer.Free;
  124. inherited;
  125. end;
  126. procedure TCustomColorSwatch.Invalidate;
  127. begin
  128. FBufferValid := False;
  129. inherited;
  130. end;
  131. procedure TCustomColorSwatch.Paint;
  132. var
  133. X, Y: Integer;
  134. OddY: Boolean;
  135. ScanLine: PColor32Array;
  136. const
  137. CCheckerBoardColor: array [Boolean] of TColor32 = ($FFA0A0A0, $FF5F5F5F);
  138. begin
  139. if not Assigned(Parent) then
  140. Exit;
  141. if not FBufferValid then
  142. begin
  143. (FBuffer.Backend as IPaintSupport).ImageNeeded;
  144. // draw checker board
  145. if not (FColor and $FF000000 = $FF000000) then
  146. begin
  147. Y := 0;
  148. while Y < FBuffer.Height do
  149. begin
  150. ScanLine := FBuffer.Scanline[Y];
  151. OddY := Odd(Y shr 2);
  152. for X := 0 to FBuffer.Width - 1 do
  153. ScanLine[X] := CCheckerBoardColor[Odd(X shr 2) = OddY];
  154. Inc(Y);
  155. end;
  156. end;
  157. // draw color
  158. FBuffer.FillRectT(0, 0, FBuffer.Width, FBuffer.Height, FColor);
  159. // eventually draw border
  160. if FBorder then
  161. begin
  162. FBuffer.FrameRectTS(0, 0, FBuffer.Width, FBuffer.Height, $DF000000);
  163. FBuffer.RaiseRectTS(1, 1, FBuffer.Width - 1, FBuffer.Height - 1, 20);
  164. end;
  165. (FBuffer.Backend as IPaintSupport).CheckPixmap;
  166. FBufferValid := True;
  167. end;
  168. FBuffer.Lock;
  169. with Canvas do
  170. try
  171. (FBuffer.Backend as IDeviceContextSupport).DrawTo(Canvas.Handle, 0, 0);
  172. finally
  173. FBuffer.Unlock;
  174. end;
  175. end;
  176. procedure TCustomColorSwatch.Resize;
  177. begin
  178. inherited;
  179. FBuffer.SetSize(Width, Height);
  180. FBufferValid := False;
  181. end;
  182. procedure TCustomColorSwatch.SetBorder(const Value: Boolean);
  183. begin
  184. if FBorder <> Value then
  185. begin
  186. FBorder := Value;
  187. Invalidate;
  188. end;
  189. end;
  190. procedure TCustomColorSwatch.SetColor(const Value: TColor32);
  191. begin
  192. if FColor <> Value then
  193. begin
  194. FColor := Value;
  195. Invalidate;
  196. end;
  197. end;
  198. procedure TCustomColorSwatch.WMEraseBkgnd(var Message: {$IFDEF FPC}TLmEraseBkgnd{$ELSE}TWmEraseBkgnd{$ENDIF});
  199. begin
  200. Message.Result := 1;
  201. end;
  202. procedure TCustomColorSwatch.WMGetDlgCode(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TWmGetDlgCode{$ENDIF});
  203. begin
  204. with Msg do
  205. Result := Result or DLGC_WANTARROWS;
  206. end;
  207. end.