Added utilities/color/contrast module. Added contrasted mixin and

get_contrast_yiq function.
This commit is contained in:
Aaron Russell 2011-01-29 18:00:09 +00:00 committed by Chris Eppstein
parent 802bc71c05
commit 620c9fe1ab
5 changed files with 22 additions and 0 deletions

View File

@ -1,3 +1,4 @@
@import "utilities/color";
@import "utilities/general";
@import "utilities/sprites";
@import "utilities/tables";

View File

@ -0,0 +1 @@
@import "color/contrast";

View File

@ -0,0 +1,8 @@
$contrasted-default-dark: #000;
$contrasted-default-light: #fff;
// Sets the specified background color and calculates a dark or light contrasted text color.
@mixin contrasted($bg, $dark:$contrasted-default-dark, $light:$contrasted-default-light){
background-color: $bg;
color: get_contrast_yiq($bg, $dark, $light);
}

View File

@ -40,6 +40,13 @@ module Compass::SassExtensions::Functions::Colors
alphastr = alpha.to_s(16).rjust(2, '0')
Sass::Script::String.new("##{alphastr}#{color.send(:hex_str)[1..-1]}".upcase)
end
# Calculates the contrast of a color using the YIQ algorithm
# Returns one of either a dark or light color based on the YIQ
def get_contrast_yiq(color, dark = Sass::Script::Color.new([0,0,0]), light = Sass::Script::Color.new([255,255,255]))
yiq = ( (color.red*299) + (color.green*587) + (color.blue*114) ) / 1000;
yiq >= 128 ? dark : light
end
private
def scale_color_value(value, amount)

View File

@ -88,6 +88,11 @@ class SassExtensionsTest < Test::Unit::TestCase
assert_equal "true", evaluate("blank(' ')")
assert_equal "true", evaluate("blank(-compass-space-list(' '))")
end
def test_get_contrast
assert_equal "white", evaluate("get_contrast_yiq(#000)")
assert_equal "black", evaluate("get_contrast_yiq(#fff)")
end
protected
def evaluate(value)