From 9551fbc29e5e0a6d0bd9e47441036c227cbc38e6 Mon Sep 17 00:00:00 2001 From: Jasper van Merle Date: Mon, 17 Sep 2018 20:05:47 +0200 Subject: [PATCH] Improve dark icon fix detection --- lib/tasks/sprites.thor | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/tasks/sprites.thor b/lib/tasks/sprites.thor index 251a9327..64dd82b4 100644 --- a/lib/tasks/sprites.thor +++ b/lib/tasks/sprites.thor @@ -95,12 +95,13 @@ class SpritesCLI < Thor def needs_dark_icon_fix(icon, bg_color) # Determine whether the icon needs to be grayscaled if the user has enabled the dark theme - # The logic comes from https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast - contrast = icon.pixels.map do |pixel| + # The logic is roughly based on https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast + contrast = icon.pixels.select {|pixel| ChunkyPNG::Color.a(pixel) > 0}.map do |pixel| get_contrast(bg_color, pixel) end - contrast.max < 7 + avg = contrast.reduce(:+) / contrast.size.to_f + avg < 3.5 end def get_contrast(base, other) @@ -173,7 +174,8 @@ class SpritesCLI < Thor def log_details(items_with_icons, icons_per_row) logger.debug("Amount of icons: #{items_with_icons.length}") - logger.debug("Icons per row: #{icons_per_row}") + logger.debug("Amount of icons needing the dark icon fix: #{items_with_icons.count {|item| item[:dark_icon_fix]}}") + logger.debug("Amount of icons per row: #{icons_per_row}") max_type_length = items_with_icons.map {|item| item[:type].length}.max border = "+#{'-' * (max_type_length + 2)}+#{'-' * 5}+#{'-' * 8}+#{'-' * 15}+"