unit ScreenNearestColor; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Spin; type TForm1 = class(TForm) LabelNearest: TLabel; SpinEditRed: TSpinEdit; SpinEditGreen: TSpinEdit; SpinEditBlue: TSpinEdit; Label2: TLabel; Label3: TLabel; Label4: TLabel; ShapeExact: TShape; ShapeNearest: TShape; Label5: TLabel; procedure SpinEditChange(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} // This function is only intended to demonstrate the concept of finding the // "nearest" color from a list. FUNCTION NearestColor(CONST color: TColor): TColor; CONST ColorList: ARRAY[0..15] OF TColor = (clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray, clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite); VAR DistanceSquared: INTEGER; B1, B2: INTEGER; G1, G2: INTEGER; i: INTEGER; R1, R2: INTEGER; SmallestDistanceSquared: INTEGER; BEGIN RESULT := clBlack; // Assume black is closest color SmallestDistanceSquared := 256*256*256; // Any distance would be shorter R1 := GetRValue(color); G1 := GetGValue(color); B1 := GetBValue(color); FOR i := 0 TO High(ColorList) DO BEGIN R2 := GetRValue(ColorList[i]); G2 := GetGValue(ColorList[i]); B2 := GetBValue(ColorList[i]); DistanceSquared := SQR(R1-R2) + SQR(G1-G2) + SQR(B1-B2); IF DistanceSquared < SmallestDistanceSquared THEN BEGIN RESULT := ColorList[i]; SmallestDistanceSquared := DistanceSquared END END END {NearestColor}; procedure TForm1.SpinEditChange(Sender: TObject); VAR Color : TColor; CloseColor: TColor; begin Color := RGB(SpinEditRed.Value, SpinEditGreen.Value, SpinEditBlue.Value); ShapeExact.Brush.Color := Color; CloseColor := NearestColor(Color); ShapeNearest.Brush.Color := CloseColor; LabelNearest.Caption := ColorToString(CloseColor) end; end.