Mega Code Archive

 
Categories / Delphi / VCL
 

Color problems in 256 color mode

Question: When I am drawing graphics with my application in 256 color mode (or less), some pen and text colors do not match my brush colors. Answer: Unless you are using a palette, Windows will attempt to convert pen and text colors to the nearest pure color available. You can determine the nearest pure color, a given color will map to, by calling the Windows API function GetNearestColor(). If you wish to use dithered colors for a pen, you can also set the pen style to the psInsideFrame style. When using this style, the pen draws lines within the frame of closed shapes that specify a bounding rectangle instead of centering the line over the shape. For the psInsideFrame style to have an effect, the pen width must be larger than 1 pixel. Example: procedure TForm1.Button1Click(Sender: TObject); var NearestColor : TColorRef; begin Form1.Canvas.Brush.Color := RGB(143, 53, 22); Form1.Canvas.Pen.Color := RGB(143, 53, 22); Form1.Canvas.Pen.Width := 10; Form1.Canvas.Ellipse(10, 10, 100, 100); Form1.Canvas.Pen.Style := psInsideFrame; Form1.Canvas.Ellipse(10, 110, 100, 200); Memo1.Lines.Add( 'The nearest displayable color to RGB(143, 53, 22) is:'); NearestColor := GetNearestColor(Form1.Canvas.Handle, RGB(143, 53, 22)); Memo1.Lines.Add('Red := ' + IntToStr(GetRValue(NearestColor))); Memo1.Lines.Add('Red := ' + IntToStr(GetGValue(NearestColor))); Memo1.Lines.Add('Blue := ' + IntToStr(GetBValue(NearestColor))); end;