Entwickler-Ecke

Multimedia / Grafik - Farbe umkehren?


StefanH - So 18.01.04 15:33
Titel: Farbe umkehren?
Hallo!

wie kann ich eine Farbe umkehren, sprich aus schwarz wird weiß oda so?
mir fällt irgendwie niks dazu ein... :?
:roll:

Stefan


Andreas Pfau - So 18.01.04 15:42

Hallo,

erstmal die Farbkomponenten ruassuchen:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var
  R, G, B: Byte;
begin
  R := MyColor and $FF;
  G := MyColor shr 8 and $FF;
  B := MyColor shr 16 and $FF;

Dann umkehren:

Delphi-Quelltext
1:
2:
3:
R := 255 - R;
G := 255 - G;
B := 255 - B;

Und wieder eine TColor draus machen:

Delphi-Quelltext
1:
MyColor := R or (G shl 8) or (B shl 16);                    

Fertig.


StefanH - So 18.01.04 16:04

danke :D


Andreas Pfau - So 18.01.04 16:12

Hallo,

:oops: aähem... sorry dass mir das jetzt erst einfällt, aber... Ich hatte mal das Problem, dass ich ein Panel hatte, dessen Farbe man ändern konnte. Darauf war ein Text - in Komplenetärfarbe. Da Problem: Bei mittlerem Grau ist die Komplenetärfarbe fast die selbe - Kontrast annähernd null.

Also habe ich damals noch nen 2. Algo entwickelt:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function Contrast(Color: TColor): TColor;
var
  R, G, B: Integer;
begin
  Color := ColorToRgb(Color);
  R := Color and $FF;
  G := (Color and $FF00) shr 8;
  B := (Color and $FF0000) shr 16;
  if ((R * 0.239) + (G * 0.686) + (B * 0.075) >= 127.5) then
    Result := clBlack
  else
    Result := clWhite;
end;

Der Rückgabewert ist entweder schwarz oder weiß - je nach dem, was grade den besseren Kontrast liefert.


StefanH - So 18.01.04 17:43

ok -- dann nehm ich des. ich hab auch ein Panel :-)