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);
|
||||
}
|
@ -41,6 +41,13 @@ module Compass::SassExtensions::Functions::Colors
|
||||
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)
|
||||
if amount > 0
|
||||
|
@ -89,6 +89,11 @@ class SassExtensionsTest < Test::Unit::TestCase
|
||||
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)
|
||||
Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s
|
||||
|
Loading…
Reference in New Issue
Block a user