Skip to content

Commit

Permalink
Improved colors brightness calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Jun 27, 2017
1 parent 953ad98 commit 474f350
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
27 changes: 18 additions & 9 deletions PersianBingCalendar/Core/PersianCalendarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace PersianBingCalendar.Core
public class PersianCalendarRenderer : IDisposable
{
private const int BingLogoRightMargin = 246;
private const int BrightnessThreshold = 130;
private const int CellMargin = 15;
private const string LargestDayStringInMonth = "31";
private const int RightMargin = 100;
private readonly int _day;
private readonly Graphics _graphics;
Expand Down Expand Up @@ -223,7 +225,7 @@ SizeF getMaxCellSize(int margin)
sizes.Add(measureString(weekDay, CalendarFontSize));
}

sizes.Add(measureString("31", CalendarFontSize));
sizes.Add(measureString(LargestDayStringInMonth, CalendarFontSize));

var maxHeight = sizes.Select(x => x.Height).Max();
var maxWidth = sizes.Select(x => x.Width).Max();
Expand Down Expand Up @@ -261,7 +263,7 @@ private void printHeaderText()
private void printHolidayTexts(IList<HolidayItem> items, Point lastPoint)
{
var space = 2;
var dayTextSize = measureString("31", HolidaysFontSize).Width;
var dayTextSize = measureString(LargestDayStringInMonth, HolidaysFontSize).Width;

foreach (var item in items)
{
Expand Down Expand Up @@ -293,9 +295,7 @@ private void printPoint(
pointY -= cellHeight - _maxCellHeight;
}

var color = fillColor == null ?
Color.FromArgb(alpha: 190, baseColor: _avgColor) :
Color.FromArgb(alpha: 110, baseColor: fillColor.Value);
var color = fillColor == null ? _avgColor : Color.FromArgb(alpha: 110, baseColor: fillColor.Value);

if (applyDarkerColor)
{
Expand All @@ -310,11 +310,10 @@ private void printPoint(
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center,
FormatFlags = isRtl ? StringFormatFlags.DirectionRightToLeft : StringFormatFlags.NoClip

};

_graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
var textColor = color.Brightness() < 140 ? Color.White : Color.Black;
var textColor = color.Brightness() < BrightnessThreshold ? Color.White : Color.Black;

if (customFont != null)
{
Expand All @@ -338,10 +337,20 @@ private void printPoint(
}
}

private void setColors()
private void setAvgColor()
{
_avgColor = _image.CropImage(new Rectangle(_leftMargin - RightMargin, 0, _image.Width, _image.Height))
_avgColor = _image.CropImage(new Rectangle(_leftMargin - RightMargin, 0, _image.Width, _image.Height / 2))
.CalculateAverageColor();
_avgColor = Color.FromArgb(alpha: 190, baseColor: _avgColor);
while (_avgColor.Brightness() >= BrightnessThreshold)
{
_avgColor = _avgColor.ChangeColorBrightness(-0.01f);
}
}

private void setColors()
{
setAvgColor();
_holidayColor = _avgColor.ChangeColorBrightness(-0.7f);
}

Expand Down
4 changes: 2 additions & 2 deletions PersianBingCalendar/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.1.0")]
[assembly: AssemblyFileVersion("1.8.1.0")]
[assembly: AssemblyVersion("1.9.0.0")]
[assembly: AssemblyFileVersion("1.9.0.0")]
11 changes: 11 additions & 0 deletions PersianBingCalendar/Utils/DrawingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static Color Blend(Color color, Color backColor, double amount)
return Color.FromArgb(r, g, b);
}

/// <summary>
/// every value in the rage 128-145 will give acceptable results:
/// Color textColor = Brightness(backgroundColor) &lt; 130 ? Colors.White : Colors.Black;
/// </summary>
public static int Brightness(this Color c)
{
return (int)Math.Sqrt(
Expand All @@ -24,6 +28,13 @@ public static int Brightness(this Color c)
c.B * c.B * .068);
}

public static bool HasEnoughContrast(Color foreColor, Color backColor, double threshold = 70)
{
var foreBrightness = Brightness(foreColor);
var backBrightness = Brightness(backColor);
return Math.Abs(foreBrightness - backBrightness) > threshold;
}

public static Color CalculateAverageColor(this Bitmap bm)
{
int width = bm.Width;
Expand Down

0 comments on commit 474f350

Please sign in to comment.