以下是我编写的函数(以及一些描述一对夫妇的评论),它们用于将“浅色”(在白纸上很难看到)颜色映射到黑色。我要求CheckSwapWhite
打印每种颜色,但可以为每个像素调用它。我没有创建灰度图像的公式,但我敢打赌,您可以通过创造性的 Google 搜索找到它。
//----------------------------------------------------------------------------
/*
Colour Brightness Formula
The following is the formula suggested by the World Wide Web
Consortium (W3C) to determine the brightness of a colour.
((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
The difference between the background brightness, and the
foreground brightness should be greater than 125.
*/
//----------------------------------------------------------------------------
int ColorBrightness( COLORREF cr )
{
return ((GetRValue(cr) * 299) +
(GetGValue(cr) * 587) +
(GetBValue(cr) * 114)) / 1000;
}
//----------------------------------------------------------------------------
/*
Colour Difference Formula
The following is the formula suggested by the W3C to determine
the difference between two colours.
(maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2))
+ (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2))
+ (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))
The difference between the background colour and the foreground
colour should be greater than 500.
*/
//----------------------------------------------------------------------------
int ColorDifference( COLORREF c1, COLORREF c2 )
{
return (max(GetRValue(c1), GetRValue(c2)) - min(GetRValue(c1), GetRValue(c2)))
+ (max(GetGValue(c1), GetGValue(c2)) - min(GetGValue(c1), GetGValue(c2)))
+ (max(GetBValue(c1), GetBValue(c2)) - min(GetBValue(c1), GetBValue(c2)));
}
//----------------------------------------------------------------------------
COLOREF CheckSwapWhite( COLORREF cr )
{
int cdiff;
int bdiff;
bdiff = 255 - ColorBrightness( cr ); // 255 = ColorBrightness(WHITE)
cdiff = ColorDifference( cr, RGB(0xFF,0xFF,0xFF) );
if( (cdiff < gnDiffColorThreshold) || // (500 by default)
(bdiff < gnDiffBrightThreshold) ) // (125 by default)
{
return RGB(0x00,0x00,0x00); // black
}
return cr;
}
这两个阈值变量是具有给定默认值的可配置设置。