unit HSLRingPicker; {$IFDEF FPC} {$MODE DELPHI} {$ENDIF} interface uses LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms, Menus, Math, Themes, RGBHSLUtils, HRingPicker, SLColorPicker, HTMLColors, mbBasicPicker; type THSLRingPicker = class(TmbBasicPicker) private FRingPicker: THRingPicker; FSLPicker: TSLColorPicker; FSelectedColor: TColor; FRValue, FGValue, FBValue: integer; FRingHint, FSLHint: string; FSLMenu, FRingMenu: TPopupMenu; FSLCursor, FRingCursor: TCursor; PBack: TBitmap; function GetHue: Integer; function GetLum: Integer; function GetSat: Integer; function GetMaxHue: Integer; function GetMaxLum: Integer; function GetMaxSat: Integer; procedure SetHue(H: integer); procedure SetSat(S: integer); procedure SetLum(L: integer); procedure SetMaxHue(H: Integer); procedure SetMaxLum(L: Integer); procedure SetMaxSat(S: Integer); procedure SetR(v: integer); procedure SetG(v: integer); procedure SetB(v: integer); procedure SetRingHint(h: string); procedure SetSLHint(h: string); procedure SetSLMenu(m: TPopupMenu); procedure SetRingMenu(m: TPopupMenu); procedure SetRingCursor(c: TCursor); procedure SetSLCursor(c: TCursor); protected procedure CreateWnd; override; procedure DoChange; override; procedure DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); function GetColorUnderCursor: TColor; override; procedure Paint; override; procedure Resize; override; procedure RingPickerChange(Sender: TObject); procedure SelectColor(c: TColor); procedure SLPickerChange(Sender: TObject); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; function GetHexColorUnderCursor: string; override; function GetSelectedHexColor: string; procedure SetFocus; override; property ColorUnderCursor; property Red: integer read FRValue write SetR; property Green: integer read FGValue write SetG; property Blue: integer read FBValue write SetB; published property Hue: integer read GetHue write SetHue default 0; property Saturation: integer read GetSat write SetSat default 240; property Luminance: integer read GetLum write SetLum default 120; property SelectedColor: TColor read FSelectedColor write SelectColor default clRed; property RingPickerPopupMenu: TPopupMenu read FRingMenu write SetRingMenu; property SLPickerPopupMenu: TPopupMenu read FSLMenu write SetSLMenu; property RingPickerHintFormat: string read FRingHint write SetRingHint; property SLPickerHintFormat: string read FSLHint write SetSLHint; property RingPickerCursor: TCursor read FRingCursor write SetRingCursor default crDefault; property SLPickerCursor: TCursor read FSLCursor write SetSLCursor default crDefault; property MaxHue: Integer read GetMaxHue write SetMaxHue default 359; property MaxLuminance: Integer read GetMaxLum write SetMaxLum default 240; property MaxSaturation: Integer read GetMaxSat write SetMaxSat default 240; property TabStop default true; property ShowHint; property ParentShowHint; property Anchors; property Align; property Visible; property Enabled; property TabOrder; property Color; property ParentColor default true; property OnChange; //: TNotifyEvent read FOnChange write FOnChange; property OnMouseMove; end; implementation {THSLRingPicker} constructor THSLRingPicker.Create(AOwner: TComponent); begin inherited; // ControlStyle := ControlStyle - [csAcceptsControls] + [csOpaque{$IFDEF DELPHI_7_UP}, csParentBackground{$ENDIF}]; FRValue := 255; FGValue := 0; FBValue := 0; PBack := TBitmap.Create; // PBack.PixelFormat := pf32bit; SetInitialBounds(0, 0, 245, 245); TabStop := true; FSelectedColor := clRed; FRingCursor := crDefault; FSLCursor := crDefault; FRingHint := 'Hue: %h'; FSLHint := 'S: %hslS L: %l'#13'Hex: %hex'; FRingPicker := THRingPicker.Create(Self); InsertControl(FRingPicker); with FRingPicker do begin SetInitialBounds(0, 0, 246, 246); //Radius := 40; Align := alClient; Visible := true; Saturation := FRingPicker.MaxSaturation; Value := FRingPicker.MaxValue; Hue := 0; OnChange := RingPickerChange; OnMouseMove := DoMouseMove; end; FSLPicker := TSLColorPicker.Create(Self); InsertControl(FSLPicker); with FSLPicker do begin SetInitialBounds(63, 63, 120, 120); MaxSaturation := 240; MaxLuminance := 240; Saturation := 240; Luminance := 240; Visible := true; OnChange := SLPickerChange; OnMouseMove := DoMouseMove; end; end; destructor THSLRingPicker.Destroy; begin PBack.Free; inherited Destroy; end; procedure THSLRingPicker.CreateWnd; begin inherited; PaintParentBack(PBack); end; procedure THSLRingPicker.DoChange; begin if (FRingPicker = nil) or (FSLPicker = nil) then exit; FRValue := GetRValue(FSLPicker.SelectedColor); FGValue := GetGValue(FSLPicker.SelectedColor); FBValue := GetBValue(FSLPicker.SelectedColor); inherited; end; procedure THSLRingPicker.DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if Assigned(OnMouseMove) then OnMouseMove(Self, Shift, x, y); inherited; end; function THSLRingPicker.GetColorUnderCursor: TColor; begin Result := FSLPicker.ColorUnderCursor; end; function THSLRingPicker.GetHexColorUnderCursor: string; begin Result := FSLPicker.GetHexColorUnderCursor; end; function THSLRingPicker.GetHue: Integer; begin Result := FRingPicker.Hue; end; function THSLRingPicker.GetLum: Integer; begin Result := FSLPicker.Luminance; end; function THSLRingPicker.GetMaxHue: Integer; begin Result := FRingPicker.MaxHue; end; function THSLRingPicker.GetMaxSat: Integer; begin Result := FSLPicker.MaxSaturation; end; function THSLRingPicker.GetMaxLum: Integer; begin Result := FSLPicker.MaxLuminance; end; function THSLRingPicker.GetSat: Integer; begin Result := FSLPicker.Saturation; end; function THSLRingPicker.GetSelectedHexColor: string; begin Result := ColorToHex(FSelectedColor); end; procedure THSLRingPicker.Paint; begin PaintParentBack(PBack); Canvas.Draw(0, 0, PBack); end; procedure THSLRingPicker.Resize; var circ: TPoint; ctr: double; begin inherited; if (FRingPicker = nil) or (FSLPicker = nil) then exit; ctr := Min(Width, Height) / 100; circ.x := Min(Width, Height) div 2; circ.y := circ.x; FRingPicker.Radius := circ.x - round(12*ctr); FSLPicker.Left := circ.x - FSLPicker.Width div 2; FSLPicker.Top := circ.y - FSLPicker.Height div 2; FSLPicker.Width := round(50 * ctr); FSLPicker.Height := FSLPicker.Width; PaintParentBack(PBack); end; procedure THSLRingPicker.RingPickerChange(Sender: TObject); begin if (FRingPicker = nil) or (FSLPicker = nil) then exit; if FSLPicker.Hue <> FRingPicker.Hue then begin FSLPicker.Hue := FRingPicker.Hue; DoChange; end; end; procedure THSLRingPicker.SelectColor(c: TColor); begin if (FRingPicker = nil) or (FSLPicker = nil) then exit; FRingPicker.Hue := GetHValue(c); //FRingPicker.Saturation := FRingPicker.MaxSaturation; //FRingPicker.Value := FRingPicker.MaxValue; FSLPicker.SelectedColor := c; FSelectedColor := c; end; procedure THSLRingPicker.SetB(v: integer); begin FBValue := v; SelectColor(RGB(FRValue, FGValue, FBValue)); end; procedure THSLRingPicker.SetFocus; begin inherited; FRingPicker.SetFocus; end; procedure THSLRingPicker.SetG(v: integer); begin FGValue := v; SelectColor(RGB(FRValue, FGValue, FBValue)); end; procedure THSLRingPicker.SetHue(H: integer); begin if (FRingPicker = nil) or (FSLPicker = nil) then exit; FRingPicker.Hue := H; FSLPicker.Hue := H; end; procedure THSLRingPicker.SetLum(L: integer); begin if (FSLPicker = nil) then exit; FSLPicker.Luminance := L; end; procedure THSLRingPicker.SetMaxHue(H: Integer); begin FRingPicker.MaxHue := H; end; procedure THSLRingPicker.SetMaxLum(L: Integer); begin FSLPicker.MaxLuminance := L; end; procedure THSLRingPicker.SetMaxSat(S: Integer); begin FSLPicker.MaxSaturation := S; end; procedure THSLRingPicker.SetR(v: integer); begin FRValue := v; SelectColor(RGB(FRValue, FGValue, FBValue)); end; procedure THSLRingPicker.SetRingCursor(c: TCursor); begin FRingCursor := c; FRingPicker.Cursor := c; end; procedure THSLRingPicker.SetRingHint(h: string); begin FRingHint := h; FRingPicker.HintFormat := h; end; procedure THSLRingPicker.SetRingMenu(m: TPopupMenu); begin FRingMenu := m; FRingPicker.PopupMenu := m; end; procedure THSLRingPicker.SetSat(S: integer); begin if (FSLPicker = nil) then exit; FSLPicker.Saturation := S; end; procedure THSLRingPicker.SetSLCursor(c: TCursor); begin FSLCursor := c; FSLPicker.Cursor := c; end; procedure THSLRingPicker.SetSLHint(h: string); begin FSLHint := h; FSLPicker.HintFormat := h; end; procedure THSLRingPicker.SetSLMenu(m: TPopupMenu); begin FSLMenu := m; FSLPicker.PopupMenu := m; end; procedure THSLRingPicker.SLPickerChange(Sender: TObject); begin if (FSLPicker <> nil) and (FSelectedColor <> FSLPicker.SelectedColor) then begin FSelectedColor := FSLPicker.SelectedColor; DoChange; end; end; end.