-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust & scale lightness and saturation
- Loading branch information
1 parent
183a67b
commit cd6ce54
Showing
5 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Compass Color Helpers | ||
crumb: Colors | ||
framework: compass | ||
meta_description: Helper function for colors. | ||
layout: core | ||
classnames: | ||
- reference | ||
- core | ||
- helpers | ||
--- | ||
%h1 Compass Color Helpers | ||
%p | ||
These color functions are useful for creating generic libraries that have to accept a | ||
range of inputs. For more color functions see | ||
<a href="http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html">the sass reference | ||
documentation</a> | ||
|
||
#adjust-lightness.helper | ||
%h3 | ||
%a(href="#adjust-lightness") | ||
adjust-lightness(<span class="arg">$color</span>, <span class="arg">$amount</span>) | ||
.details | ||
%p | ||
Adds <code>$amount</code> to <code>$color</code>'s lightness value. <code>$amount</code> | ||
can be negative. | ||
|
||
#adjust-saturation.helper | ||
%h3 | ||
%a(href="#adjust-saturation") | ||
adjust-saturation(<span class="arg">$color</span>, <span class="arg">$amount</span>) | ||
.details | ||
%p | ||
Adds <code>$amount</code> to <code>$color</code>'s saturation value. <code>$amount</code> | ||
can be negative. | ||
|
||
#scale-lightness.helper | ||
%h3 | ||
%a(href="#scale-lightness") | ||
scale-lightness(<span class="arg">$color</span>, <span class="arg">$amount</span>) | ||
.details | ||
%p | ||
Scales <code>$color</code>'s lightness value by <code>$amount</code>. | ||
<code>$amount</code> can be negative. | ||
|
||
#scale-saturation.helper | ||
%h3 | ||
%a(href="#scale-saturation") | ||
scale-saturation(<span class="arg">$color</span>, <span class="arg">$amount</span>) | ||
.details | ||
%p | ||
Scales <code>$color</code>'s saturation value by <code>$amount</code>. | ||
<code>$amount</code> can be negative. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Compass::SassExtensions::Functions::Colors | ||
|
||
# a genericized version of lighten/darken so that negative values can be used. | ||
def adjust_lightness(color, amount) | ||
assert_type color, :Color | ||
assert_type amount, :Number | ||
color.with(:lightness => Sass::Util.restrict(color.lightness + amount.value, 0..100)) | ||
end | ||
|
||
# Scales a color's lightness by some percentage. | ||
# If the amount is negative, the color is scaled darker, if positive, it is scaled lighter. | ||
# This will never return a pure light or dark color unless the amount is 100%. | ||
def scale_lightness(color, amount) | ||
assert_type color, :Color | ||
assert_type amount, :Number | ||
color.with(:lightness => scale_color_value(color.lightness, amount.value)) | ||
end | ||
|
||
# a genericized version of saturation/desaturate so that negative values can be used. | ||
def adjust_saturation(color, amount) | ||
assert_type color, :Color | ||
assert_type amount, :Number | ||
color.with(:saturation => Sass::Util.restrict(color.saturation + amount.value, 0..100)) | ||
end | ||
|
||
# Scales a color's saturation by some percentage. | ||
# If the amount is negative, the color is desaturated, if positive, it is saturated. | ||
# This will never return a pure saturated or desaturated color unless the amount is 100%. | ||
def scale_saturation(color, amount) | ||
assert_type color, :Color | ||
assert_type amount, :Number | ||
color.with(:saturation => scale_color_value(color.saturation, amount.value)) | ||
end | ||
|
||
private | ||
def scale_color_value(value, amount) | ||
if amount > 0 | ||
value += (100 - value) * (amount / 100.0) | ||
elsif amount < 0 | ||
value += value * amount / 100.0 | ||
end | ||
value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters