unit UPersoFrame;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Couleur, Registry;

type
  TFramePerso = class(TFrame)
    GBDelphi: TGroupBox;
    GBGen: TGroupBox;
    GBSpe: TGroupBox;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Image5: TImage;
    Image6: TImage;
    Image7: TImage;
    Image8: TImage;
    Image9: TImage;
    Image10: TImage;
    Image11: TImage;
    Image12: TImage;
    Image13: TImage;
    Image14: TImage;
    Image15: TImage;
    Image16: TImage;
    Image17: TImage;
    Image18: TImage;
    Image19: TImage;
    Image20: TImage;
    Image21: TImage;
    Image22: TImage;
    Image23: TImage;
    Image24: TImage;
    Image25: TImage;
    Image26: TImage;
    Image27: TImage;
    Image28: TImage;
    Image29: TImage;
    Image30: TImage;
    Image31: TImage;
    Image32: TImage;
    Image33: TImage;
    Image34: TImage;
    Image35: TImage;
    Image36: TImage;
    Image37: TImage;
    Image38: TImage;
    Image39: TImage;
    Image40: TImage;
    Image41: TImage;
    Image42: TImage;
    Image43: TImage;
    Image44: TImage;
    Image45: TImage;
    Image46: TImage;
    Image47: TImage;
    Image48: TImage;
    Image49: TImage;
    Image50: TImage;
    Image51: TImage;
    Image52: TImage;
    Image53: TImage;
    Image54: TImage;
    Image55: TImage;
    Image56: TImage;
    Image57: TImage;
    Image58: TImage;
    Image59: TImage;
    Image60: TImage;
    Image61: TImage;
    Image62: TImage;
    Image63: TImage;
    Image64: TImage;
    Image65: TImage;
    Image66: TImage;
    Image67: TImage;
    Image68: TImage;
    Image69: TImage;
    Image70: TImage;
    Image71: TImage;
    Image72: TImage;
    Image73: TImage;
    Image74: TImage;
    Image75: TImage;
    Image76: TImage;
    Image77: TImage;
    Image78: TImage;
    Image79: TImage;
    Image80: TImage;
    Image81: TImage;
    Image82: TImage;
    Image83: TImage;
    Image84: TImage;
    Image85: TImage;
    Image86: TImage;
    Image87: TImage;
    Image88: TImage;
    Image89: TImage;
    Image90: TImage;
    Image91: TImage;
    Image92: TImage;
    Image93: TImage;
    Image94: TImage;
    Image95: TImage;
    Image96: TImage;
    Image97: TImage;
    Image98: TImage;
    Image99: TImage;
    Image100: TImage;
    Image101: TImage;
    Image102: TImage;
    Image103: TImage;
    Image104: TImage;
    Image105: TImage;
    Image106: TImage;
    Image107: TImage;
    Image108: TImage;
    Image109: TImage;
    Image110: TImage;
    Image111: TImage;
    Image112: TImage;
    Image113: TImage;
    Image114: TImage;
    Image115: TImage;
    Image116: TImage;
    Image117: TImage;
    Image118: TImage;
    Image119: TImage;
    Image120: TImage;
    Image121: TImage;
    Image122: TImage;
    Image123: TImage;
    Image124: TImage;
    Image125: TImage;
    Image126: TImage;
    Image127: TImage;
    Image128: TImage;
    Image129: TImage;
    Image130: TImage;
    Image131: TImage;
    Image132: TImage;
    Image133: TImage;
    Image134: TImage;
    Image135: TImage;
    Image136: TImage;
    Image137: TImage;
    Image138: TImage;
    Image139: TImage;
    Image140: TImage;
    Image141: TImage;
    Image142: TImage;
    Image143: TImage;
    Image144: TImage;
    procedure ImageClick(Sender: TObject);
  private
    { Déclarations privées }
    procedure LoadFromRegistry;
    Procedure SaveToRegestry;
  public
    { Déclarations publiques }
    Old : TImage;
    Couleur : TCouleur;
    procedure DrawCoul(Img : TImage;Sel:Boolean=false);
    procedure CreateFrame;
    procedure FreeFrame;
    procedure InitPerso;
  end;

implementation

{$R *.dfm}

procedure TFramePerso.CreateFrame;
var
  I : Integer;
begin
  Couleur := TCouleur.Create;
  for I := 0 to GBDelphi.ControlCount-1 do
    if GBDelphi.Controls[I] is Timage then
      TImage(GBDelphi.Controls[I]).Canvas.Brush.Color := TImage(GBDelphi.Controls[I]).Tag;
  for I := 0 to GBGen.ControlCount-1 do
    if GBGen.Controls[I] is Timage then
    begin
      TImage(GBGen.Controls[I]).Enabled := False;
      TImage(GBGen.Controls[I]).Tag := -1;
    end;
  for I := 0 to GBSpe.ControlCount-1 do
    if GBSpe.Controls[I] is Timage then
    begin
      TImage(GBSpe.Controls[I]).Enabled := False;
      TImage(GBSpe.Controls[I]).Tag := -1;
    end;
  LoadFromRegistry;
end;

procedure TFramePerso.DrawCoul(Img: TImage; Sel: Boolean);
var
  Coul : TColor;
begin
  Coul := Img.Canvas.Brush.Color;
  //Cadre noir de sélection
  Img.Canvas.brush.Color := ColorToRGB(clBtnFace);
  if Sel = true then
  begin
    Img.Canvas.Pen.Color := $0;
    Rectangle(Img.Canvas.Handle,0,0, Img.Width,Img.height);
  end
  else
  begin
  //Couleur fond
    Img.Canvas.Pen.Color := ColorToRGB(clBtnFace);
    Rectangle(Img.Canvas.Handle,0,0, Img.Width,Img.height);
  end;
  //Cadre blanc
  Img.Canvas.Pen.Color := $FFFFFF;
  Rectangle(Img.Canvas.Handle,1,1, Img.Width-1,Img.height-1);
  //Cadre gris moyen
  Img.Canvas.Pen.Color := $808080;
  Rectangle(Img.Canvas.Handle,1,1, Img.Width-2,Img.height-2);
  //Cadre gris clair
  Img.Canvas.Pen.Color := $C8D0D4;
  Rectangle(Img.Canvas.Handle,2,2, Img.Width-2,Img.height-2);
  //Cadre gris foncé
  Img.Canvas.Pen.Color := $404040;
  Rectangle(Img.Canvas.Handle,2,2, Img.Width-3,Img.height-3);
  //Couleur de l'img
  Img.Canvas.brush.Color := Coul;
  Img.Canvas.Pen.Color := Coul;
  Rectangle(Img.Canvas.Handle,3,3, Img.Width-3,Img.height-3);
  Img.Repaint;
end;

procedure TFramePerso.FreeFrame;
begin
  Couleur.Free;
  SaveToRegestry;
end;

procedure TFramePerso.ImageClick(Sender: TObject);
begin
  if old<> nil then
    DrawCoul(Old,False);
  DrawCoul(TImage(Sender),True);
  Old := TImage(Sender);
  Couleur.Color := Old.Canvas.Brush.Color;
end;

procedure TFramePerso.InitPerso;
var
  I : Integer;
begin
  for I := 0 to GBDelphi.ControlCount-1 do
    if GBDelphi.Controls[I] is Timage then
      DrawCoul(TImage(GBDelphi.Controls[I]));
  for I := 0 to GBGen.ControlCount-1 do
    if GBGen.Controls[I] is Timage then
      DrawCoul(TImage(GBGen.Controls[I]));
  for I := 0 to GBSpe.ControlCount-1 do
    if GBSpe.Controls[I] is Timage then
      DrawCoul(TImage(GBSpe.Controls[I]));
end;

procedure TFramePerso.LoadFromRegistry;
var
  ReadReg : TRegistry;
  Data    : array[0..47]of Integer;
  I       : Integer;
begin
  ReadReg := TRegistry.Create;
  try
    ReadReg.RootKey := HKEY_CURRENT_USER;
    if ReadReg.OpenKey('\Software\nSeries\nView\ColorAdvance', False) then
    begin
      ReadReg.ReadBinaryData('Gen',Data, 48*sizeof(Integer));
      for I := 0 to GBGen.ControlCount-1 do
      begin
        TImage(GBGen.Controls[I]).Tag := Data[I];
        if Data[I]<> -1 then
        begin
          TImage(GBGen.Controls[I]).Canvas.Brush.Color := data[I];
          TImage(GBGen.Controls[I]).Enabled := True;
        end;
      end;
      if ReadReg.ReadBinaryData(ExtractFileName(Application.ExeName),Data, 48*sizeof(Integer))>0 then
      for I := 0 to GBSpe.ControlCount-1 do
      begin
        TImage(GBSpe.Controls[I]).Tag := Data[I];
        if Data[I]<> -1 then
        begin
          TImage(GBSpe.Controls[I]).Canvas.Brush.Color := data[I];
          TImage(GBSpe.Controls[I]).Enabled := True;
        end;
      end;
      ReadReg.CloseKey;
    end;
  finally
    ReadReg.Free;
  end;
end;

procedure TFramePerso.SaveToRegestry;
var
  WriteReg     : TRegistry;
  Data, Data2 : array[0..47]of Integer;
  I : Integer;
begin
  for I := 0 to GBGen.ControlCount-1 do
  begin
    Data[I] := TImage(GBGen.Controls[I]).Tag;
    Data2[I] := TImage(GBSpe.Controls[I]).Tag;
  end;
  WriteReg := TRegistry.Create;
  try
    WriteReg.RootKey := HKEY_CURRENT_USER;
    if WriteReg.OpenKey('\Software\nSeries\nView\ColorAdvance', True) then
    begin
      WriteReg.WriteBinaryData('Gen',Data, 48*sizeof(Integer));
      WriteReg.WriteBinaryData(ExtractFileName(Application.ExeName),Data2, 48*sizeof(Integer));
      WriteReg.CloseKey;
    end;
  finally
    WriteReg.Free;
  end;
end;

end.
