unit ColorDialog;

interface

uses
  SysUtils, Classes, Graphics;

type
  TColorDialog = class(TComponent)
  private const
    DEFAULT_COLOR = clRed;
  private var
    FColor: TColor;
    function GetColorAsHex: string;
    procedure SetColorAsHex(const Value: string);
  protected
  public
    function Execute: boolean;
    constructor Create(AOwner: TComponent); override;
  published
    property Color: TColor read FColor write FColor default DEFAULT_COLOR;
    property ColorAsHex: string read GetColorAsHex write SetColorAsHex;
  end;

procedure Register;

implementation

uses Controls, RColors, colorWin;

procedure Register;
begin
  RegisterComponents('Rejbrand 2015', [TColorDialog]);
end;

{ TColorDialog }

constructor TColorDialog.Create(AOwner: TComponent);
begin
  inherited;
  FColor := DEFAULT_COLOR;
end;

function TColorDialog.Execute: boolean;
begin
  with TcolorDlg.Create(Owner) do
    try
      Color := Self.Color;
      result := ShowModal = mrOk;
      if result then
        Self.Color := Color;
    finally
      Free;
    end;
end;

function TColorDialog.GetColorAsHex: string;
begin
  result := '#' + IntToHex(FColor and $0000FF, 2) +
    IntToHex((FColor and $00FF00) shr 8, 2) +
    IntToHex((FColor and $FF0000) shr 16, 2);
end;

procedure TColorDialog.SetColorAsHex(const Value: string);
begin
  FColor := HexColorCodeToColor(Value);
  if FColor = clNone then
    FColor := DEFAULT_COLOR;
end;

end.
