Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Add extra color methods:
Browse files Browse the repository at this point in the history
? saturateColor(color, amount)
? desaturateColor(color, amount)
? greyscale(color)
? lighten(color, amount)
? darken(color, amount)
? spin(color, hue_angle)

Contributed by Yves Brissaud (@eunomie on GitHub)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=107215377
  • Loading branch information
eunomie authored and iflan committed Nov 6, 2015
1 parent f2d1877 commit 5e681fe
Show file tree
Hide file tree
Showing 4 changed files with 606 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ for details, but here are the functions and the arguments that they take:
* `addHsbToCssColor(baseColor, hueToAdd, saturationToAdd, brightnessToAdd)`
* `makeContrastingColor(color, similarityIndex)`
* `adjustBrightness(color, brightness)`
* `saturateColor(color, saturationToAdd)` increase saturation in HSL color space
* `desaturateColor(color, saturationToRemove)` decrease saturation in HSL color space
* `greyscale(color)` full desaturation of a color in HSL color space
* `lighten(color, lightnessToAdd)` increase the lightness in HSL color space
* `darken(color, lightnessToRemove)` decrease the lightness in HSL color space
* `spin(color, hueAngle)` increase or decrease hue of the color, like rotating in a color wheel

There is also a `selectFrom()` function that behaves like the ternary operator:

Expand Down Expand Up @@ -554,6 +560,7 @@ to create **`dialog.soy.js`**:
```
java -jar SoyToJsSrcCompiler.jar \\
--shouldProvideRequireSoyNamespaces \\
--codeStyle concat \\
--cssHandlingScheme GOOG \\
--outputPathFormat '{INPUT_FILE_NAME_NO_EXT}.soy.js' \\
dialog.soy
Expand Down
72 changes: 72 additions & 0 deletions src/com/google/common/css/compiler/gssfunctions/ColorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.css.compiler.gssfunctions;

import java.awt.Color;
import java.lang.Math;

/**
* Utility functions to deal with colors.
Expand All @@ -25,9 +26,14 @@
*/
class ColorUtil {

/** Index of Hue in HSB and HSL array. */
static final int H = 0;
/** Index of Saturation in HSB and HSL array. */
static final int S = 1;
/** Index of Brightness in HSB array. */
static final int B = 2;
/** Index of Lightness in HSL array. */
static final int L = 2;

static float[] toHsb(Color color) {
return Color.RGBtoHSB(
Expand All @@ -43,6 +49,72 @@ static Color hsbToColor(float[] inputHsb) {
return Color.getHSBColor(inputHsb[H], inputHsb[S], inputHsb[B]);
}

/**
* Convert a color in HSB color space to one in HSL color space.
*
* @param inputHsb HSB color in a array of three floats, 0 is Hue, 1 is
* Saturation and 2 is Brightness
* @return HSL color in array of three floats, 0 is Hue, 1 is
* Saturation and 2 is Lightness
*/
static float[] hsbToHsl(float[] inputHsb) {
float hHsb = inputHsb[H];
float sHsb = inputHsb[S];
float bHsb = inputHsb[B];

float hHsl = hHsb;
float lHsl = bHsb * (2 - sHsb) / 2;
float sHsl = bHsb * sHsb / (1 - Math.abs(2 * lHsl - 1));

float[] hsl = {hHsl, sHsl, lHsl};

return hsl;
}

/**
* Get the HSL values of a color.
*
* @param color Color to get the HSL values
* @return array of floats representing the color in HSL color space
*/
static float[] toHsl(Color color) {
return hsbToHsl(toHsb(color));
}

/**
* Convert a color in HSL color space to one in HSB color space.
*
* @param inputHsl HSL color in a array of three floats, 0 is Hue, 1 is
* Saturation and 2 is Lightness
* @return HSB color in array of three floats, 0 is Hue, 1 is
* Saturation and 2 is Brightness
*/
static float[] hslToHsb(float[] inputHsl) {
float hHsl = inputHsl[H];
float sHsl = inputHsl[S];
float lHsl = inputHsl[L];

float hHsb = hHsl;
float bHsb = (2 * lHsl + sHsl * (1 - Math.abs(2 * lHsl - 1))) / 2;
float sHsb = 2 * (bHsb - lHsl) / bHsb;

float[] hsb = {hHsb, sHsb, bHsb};

return hsb;
}

/**
* Get the color from the HSL floats
*
* @param inputHsl HSL color
* @return Java color
*/
static Color hslToColor(float[] inputHsl) {
float[] hsb = hslToHsb(inputHsl);

return Color.getHSBColor(hsb[H], hsb[S], hsb[B]);
}

/**
* Tests whether the given colors are contrasting colors, according to the
* test described in the W3C accessibility evaluation working draft
Expand Down
Loading

0 comments on commit 5e681fe

Please sign in to comment.