Added utilities/color/contrast module. Added contrasted mixin and
get_contrast_yiq function.
This commit is contained in:
parent
802bc71c05
commit
620c9fe1ab
@ -1,3 +1,4 @@
|
||||
@import "utilities/color";
|
||||
@import "utilities/general";
|
||||
@import "utilities/sprites";
|
||||
@import "utilities/tables";
|
||||
|
@ -0,0 +1 @@
|
||||
@import "color/contrast";
|
@ -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);
|
||||
}
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user