Autor Beitrag
Waldkauz
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 102



BeitragVerfasst: So 28.06.09 14:22 
Hallo

Ich möchte einem Shape eine Zufallsfarbe geben, allerdings nur aus den Farben rot, gelb, blau, grün, weiß, schwarz

wenn ich jetzt nehme rgb(random(256),random(256),random(256);, dann nimmt er ja auch Farben wie hellrot, dunkelgelb, hellblau etc., ich möchte aber nur die reinen Farben
Tropby
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 71
Erhaltene Danke: 4

Vista, Win XP, Win 89
Turbo Delphi Ex.
BeitragVerfasst: So 28.06.09 14:27 
Wenn du nur diese Farben hast dann könntest du das ja so machen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
case Random(6) + 1 of
1: farbe := clred;
2: farbe := clyellow;
3: farbe := clblue;
4: farbe := clgreen;
5: farbe := clwhite;
6: farbe := clblack;
end;

_________________
Tropby
Waldkauz Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 102



BeitragVerfasst: So 28.06.09 14:30 
Moderiert von user profile iconNarses: Komplett-Zitat des letzten Beitrags entfernt.

Genau das ist mir gerade eingefallen ^^ - danke
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 28.06.09 14:43 
Einfacher:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
const
  MyColors: array[0..4of TColor = (clRed, clGreen, clBlue, clYellow, clWhite);
begin
  CurrentColor := MyColors[Random(Length(MyColors))];
  ...
end;
R4id
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: So 28.06.09 18:36 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function RandomColor(const Colors: array of TColor): TColor;
var 
  I: Integer;
begin
  Result := clBlack;

  if Length(Colors) <> 0 then
    Result := Colors[Random(High(Colors))];
end;


Wäre es nicht so besser?
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: So 28.06.09 20:55 
Nein, denn du solltest nicht einfach clblack zurückgeben. Dafür Sind Schließlich exceptions da.
Aber ansonsten wohl ganz ok.