Enhance readability of sorted columns in high contrast mode (white text on black background). Check brightness and decide for a brighter or darker background color as needed. See issue #1366.

This commit is contained in:
Ansgar Becker
2009-12-01 00:17:21 +00:00
parent 7fed732f40
commit 1e0f6444b8
3 changed files with 44 additions and 35 deletions

View File

@ -196,7 +196,8 @@ const
DEFAULT_FIELDCOLOR_DATETIME = $00000080; // clMaroon
DEFAULT_FIELDCOLOR_ENUM = $00008080; // clOlive
DEFAULT_FIELDCOLOR_SET = $00808000; // clTeal
COLORSHIFT_NULLFIELDS = 45; // Percentage of brightness to add to normal field colors for NULL values
COLORSHIFT_NULLFIELDS = 70; // Shift brightness to add to normal field colors for NULL values
COLORSHIFT_SORTCOLUMNS = 12; // Shift brightness to add to normal field colors for NULL values
REGNAME_FIELDEDITOR_BINARY = 'FieldEditor_Binary';
REGNAME_FIELDEDITOR_DATETIME = 'FieldEditor_Datetime';
@ -261,8 +262,6 @@ const
{PetaByte} NAME_PB = ' PB';
// Used by ListViews and Grids
COLOR_SORTCOLUMN_ASC = $00F7F7F7; // light grey
COLOR_SORTCOLUMN_DESC = $00EEEEEE; // medium grey
ORDER_ASC = 0; // Used for tag-value of "Direction"-button
ORDER_DESC = 1; // dito
TXT_ASC = 'ASC'; // Used for caption of "Direction"-button

View File

@ -8,7 +8,7 @@ unit helpers;
interface
uses Classes, SysUtils, Graphics, db, clipbrd, dialogs,
uses Classes, SysUtils, Graphics, GraphUtil, db, clipbrd, dialogs,
forms, controls, ShellApi, checklst, windows,
shlobj, ActiveX, WideStrUtils, VirtualTrees, SynRegExpr, Messages, WideStrings,
TntCheckLst, Registry, SynEditHighlighter, mysql_connection, mysql_structures, DateUtils;
@ -172,7 +172,7 @@ type
function BinToWideHex(bin: string): WideString;
procedure CheckHex(text: WideString; errorMessage: string);
procedure FixVT(VT: TVirtualStringTree);
function ColorAdjustBrightness(Col: TColor; ShiftPercent: ShortInt): TColor;
function ColorAdjustBrightness(Col: TColor; Shift: SmallInt; BestFit: Boolean): TColor;
function ComposeOrderClause(Cols: TOrderColArray): WideString;
procedure OpenRegistry(Session: String = '');
function GetRegValue( valueName: String; defaultValue: Integer; Session: String = '' ) : Integer; Overload;
@ -187,6 +187,8 @@ type
function DateBackFriendlyCaption(d: TDateTime): String;
procedure InheritFont(AFont: TFont);
function GetTableSize(Results: TMySQLQuery): Int64;
function GetLightness(AColor: TColor): Byte;
var
MainReg : TRegistry;
@ -2641,26 +2643,20 @@ begin
end;
function ColorAdjustBrightness(Col: TColor; ShiftPercent: ShortInt): TColor;
function ColorAdjustBrightness(Col: TColor; Shift: SmallInt; BestFit: Boolean): TColor;
var
r, g, b, ShiftBytes: SmallInt;
Lightness: Byte;
begin
// Split colors
r := GetRValue(Col);
g := GetGValue(Col);
b := GetBValue(Col);
// Convert percent value to bytes
ShiftBytes := Round(ShiftPercent * 255 / 100);
// Adjust single colors
r := r + ShiftBytes;
g := g + ShiftBytes;
b := b + ShiftBytes;
// Fix exceeded bounds
if r > 255 then r := 255 else if r < 0 then r := 0;
if g > 255 then g := 255 else if g < 0 then g := 0;
if b > 255 then b := 255 else if b < 0 then b := 0;
// Merge result
Result := RGB(r, g, b);
if BestFit then begin
// If base color is bright, make bg color darker (grey), and vice versa, so that
// colors work with high contrast mode for accessibility
Lightness := GetLightness(Col);
if (Lightness < 128) and (Shift < 0) then
Shift := Abs(Shift)
else if (Lightness > 128) and (Shift > 0) then
Shift := 0 - Abs(Shift);
end;
Result := ColorAdjustLuma(Col, Shift, true);
end;
@ -2901,6 +2897,21 @@ begin
end;
function GetLightness(AColor: TColor): Byte;
var
R, G, B: Byte;
MaxValue, MinValue: Double;
Lightness: Double;
begin
R := GetRValue(ColorToRGB(AColor));
G := GetGValue(ColorToRGB(AColor));
B := GetBValue(ColorToRGB(AColor));
MaxValue := Max(Max(R,G),B);
MinValue := Min(Min(R,G),B);
Lightness := (((MaxValue + MinValue) * 240) + 255 ) / 510;
Result := Round(Lightness);
end;
end.

View File

@ -3178,9 +3178,10 @@ begin
// Sorting color and title image
for k:=0 to Length(FDataGridSort)-1 do begin
if FDataGridSort[k].ColumnName = name then begin
col.Color := ColorAdjustBrightness(col.Color, COLORSHIFT_SORTCOLUMNS, True);
case FDataGridSort[k].SortDirection of
ORDER_ASC: begin col.Color := COLOR_SORTCOLUMN_ASC; col.ImageIndex := 109; end;
ORDER_DESC: begin col.Color := COLOR_SORTCOLUMN_DESC; col.ImageIndex := 110; end;
ORDER_ASC: col.ImageIndex := 109;
ORDER_DESC: col.ImageIndex := 110;
end;
end;
end;
@ -5387,18 +5388,16 @@ end;
procedure TMainForm.vstAfterPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas);
var
i : Integer;
h : TVTHeader;
i: Integer;
h: TVTHeader;
NewColor: TColor;
begin
h := (Sender as TVirtualStringTree).Header;
for i := 0 to h.Columns.Count - 1 do
begin
for i:=0 to h.Columns.Count-1 do begin
NewColor := clWindow;
if h.SortColumn = i then
case h.SortDirection of
sdAscending: h.Columns[i].Color := COLOR_SORTCOLUMN_ASC;
sdDescending: h.Columns[i].Color := COLOR_SORTCOLUMN_DESC;
end else
h.Columns[i].Color := clWindow;
NewColor := ColorAdjustBrightness(NewColor, COLORSHIFT_SORTCOLUMNS, True);
h.Columns[i].Color := NewColor;
end;
end;
@ -6578,7 +6577,7 @@ var
i: Integer;
begin
for i:=Low(DatatypeCategories) to High(DatatypeCategories) do
DatatypeCategories[i].NullColor := ColorAdjustBrightness(DatatypeCategories[i].Color, COLORSHIFT_NULLFIELDS);
DatatypeCategories[i].NullColor := ColorAdjustBrightness(DatatypeCategories[i].Color, COLORSHIFT_NULLFIELDS, False);
end;