Created
July 8, 2015 14:57
-
-
Save fiinix/05e38f216c8ec19f002c to your computer and use it in GitHub Desktop.
Polarize
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
/* | |
* =========================================================================== | |
* | |
* Polarize | |
* | |
* =========================================================================== | |
* | |
* Calculates the perceived brightness by measuring weighted distance | |
* in 3D RGB space | |
* | |
* Based on the work from: | |
* http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx | |
* | |
* http://cadik.posvete.cz/color_to_gray_evaluation/ | |
* | |
*/ | |
/** | |
* Default variables. | |
*/ | |
$black: #222 !default; | |
$white: #eee !default; | |
/* Polarize variables | |
========================================================================== */ | |
$polarize-black: $black !default; | |
$polarize-white: $white !default; | |
/* Every value in the range of 128-145 gives acceptable results */ | |
$polarize-cutoff: 130 !default; | |
$polarize-red-coefficients: 241 !default; | |
$polarize-green-coefficients: 691 !default; | |
$polarize-blue-coefficients: 68 !default; | |
$polarize-brightness-divisor: $polarize-red-coefficients + $polarize-green-coefficients + $polarize-blue-coefficients; | |
$polarize-cutoff: round((strip-units(lightness($polarize-white)) - strip-units(lightness($polarize-black))) * ($polarize-cutoff / 200)) +0%; | |
@function brightness($polarize-color) { | |
// Extract color components | |
$polarize-red-component: red($polarize-color); | |
$polarize-green-component: green($polarize-color); | |
$polarize-blue-component: blue($polarize-color); | |
// Calculate a brightness value in 3d color space between 0 and 255 | |
$number: sqrt((($polarize-red-component * $polarize-red-component * $polarize-red-coefficients) + ($polarize-green-component * $polarize-green-component * $polarize-green-coefficients) + ($polarize-blue-component * $polarize-blue-component * $polarize-blue-coefficients)) / $polarize-brightness-divisor); | |
// Convert to percentage and return | |
@return 100% * $number / 255; | |
} | |
@function contrast($polarize-color, $w: $polarize-white, $b: $polarize-black) { | |
@if brightness($polarize-color) < $polarize-cutoff { | |
@return $w; | |
} | |
@else { | |
@return $b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment