Ho provato anche io ad implementare la funzione che ho trovato, e devo dire che funziona benone, ti posto il .pas che ho buttato giù in 5 min 7 pannelli, 7 label e 1 bottone, i tag sia dei pannelli che delle label vanno da 1 a 7
unit Unit21;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm21 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
Button3: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
aDummyIndex: Array[1..7] of Integer;
procedure scrivi;
procedure shuffle;
{ Private declarations }
public
{ Public declarations }
end;
var
Form21: TForm21;
const
SHUFFTIME = 50;
NUMCOLOR = 7;
aColor: Array[1..NUMCOLOR] of TColor = (clBlacK, clRed, clyellow, clWhite,
clGrayText, clMaroon, clBlue);
aCaption: Array[1..NUMCOLOR] of String = ('Nero', 'Rosso', 'Giallo', 'Bianco',
'Grigio', 'Marrone', 'Blu');
implementation
{$R *.dfm}
procedure TForm21.shuffle;
var
Index, x: Integer;
index1, index2: Integer;
begin
Randomize;
for x := 0 to SHUFFTIME do
begin
index1 := Random(NUMCOLOR) + 1;
index2 := Random(NUMCOLOR) + 1;
Index := aDummyIndex[index1];
aDummyIndex[index1] := aDummyIndex[index2];
aDummyIndex[index2] := Index;
end;
end;
procedure TForm21.Button3Click(Sender: TObject);
begin
shuffle;
Scrivi;
end;
function GetContrastColorBlackOrWhite(const AColor: TColor): TColor;
var
R, G, B: single;
begin
R := GetRValue(AColor) * 0.25;
G := GetGValue(AColor) * 0.625;
B := GetBValue(AColor) * 0.125;
if (R + G + B) > 128 then
result := clBlack
else
result := clWhite;
end;
Procedure TForm21.scrivi;
var
x: Integer;
begin
for x := 0 to ControlCount - 1 do
begin
if (Controls[x] is TPanel) then
begin
(Controls[x] as TPanel).color := aColor[aDummyIndex[(Controls[x] as TPanel).tag]];
(Controls[x] as TPanel).caption := aCaption[aDummyIndex[(Controls[x] as TPanel).tag]];
(Controls[x] as TPanel).Font.Color := GetContrastColorBlackOrWhite(aColor[aDummyIndex[(Controls[x] as TPanel).tag]]);
end
else if (Controls[x] is TLabel) then
(Controls[x] as TLabel).Caption := aCaption[aDummyIndex[(Controls[x] as TLabel).tag]];
end;
end;
procedure TForm21.FormCreate(Sender: TObject);
var I: Integer;
begin
for i := Low(aDummyIndex) to High(aDummyIndex) do
aDummyIndex[i]:=i;
scrivi;
end;
end.