Skip to content

Commit

Permalink
Add spin manipulation function
Browse files Browse the repository at this point in the history
  • Loading branch information
eunomie committed Oct 27, 2015
1 parent a2be4bb commit f209009
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/com/google/common/css/compiler/gssfunctions/GssFunctions.java
Expand Up @@ -88,6 +88,7 @@ public static Map<String, GssFunction> getFunctionMap() {
.put("greyscale", new Greyscale())
.put("lighten", new Lighten())
.put("darken", new Darken())
.put("spin", new Spin())

// Logic functions.
.put("selectFrom", new SelectFrom())
Expand Down Expand Up @@ -731,7 +732,6 @@ public String getCallResultString(List<String> args)
/**
* Increase the lightness of a color. First argument is the color, second
* is the lighten to add, between 0 and 100.
*
*/
public static class Lighten extends BaseHslColorManipulation implements GssFunction {

Expand Down Expand Up @@ -794,7 +794,6 @@ public String getCallResultString(List<String> args)
/**
* Decrease the lightness of a color. First argument is the color, second
* is the lighten to remove, between 0 and 100.
*
*/
public static class Darken extends BaseHslColorManipulation implements GssFunction {
@Override
Expand Down Expand Up @@ -853,6 +852,68 @@ public String getCallResultString(List<String> args)
}
}

/**
* Increase or decrease the hue of a color. First argument is the color, second
* is the hue to add or remove, between 0 and 360.
* It's like rotating the color on a color wheel and hue is the angle to apply.
*/
public static class Spin extends BaseHslColorManipulation implements GssFunction {
@Override
public Integer getNumExpectedArguments() {
return 2;
}

@Override
public List<CssValueNode> getCallResultNodes(List<CssValueNode> args,
ErrorManager errorManager) throws GssFunctionException {
CssValueNode arg1 = args.get(0);
CssValueNode arg2 = args.get(1);

CssHexColorNode hexColor = null;
if (!(arg1 instanceof CssHexColorNode
|| arg1 instanceof CssLiteralNode)) {
String message =
"The first argument must be a CssHexColorNode or a CssLiteralNode.";
errorManager.report(
new GssError(message, arg1.getSourceCodeLocation()));
throw new GssFunctionException(message);
}
CssNumericNode numeric2;
if (arg2 instanceof CssNumericNode) {
numeric2 = (CssNumericNode)arg2;
} else {
String message = "The second argument must be a CssNumericNode";
errorManager.report(
new GssError(message, arg2.getSourceCodeLocation()));
throw new GssFunctionException(message);
}

try {
String resultString =
addHslToCssColor(args.get(0).getValue(),
numeric2.getNumericPart(),
"0",
"0");

CssHexColorNode result = new CssHexColorNode(resultString,
arg1.getSourceCodeLocation());
return ImmutableList.of((CssValueNode)result);
} catch (GssFunctionException e) {
errorManager.report(
new GssError(e.getMessage(), arg2.getSourceCodeLocation()));
throw e;
}
}

@Override
public String getCallResultString(List<String> args)
throws GssFunctionException {
String baseColorString = args.get(0);
return addHslToCssColor(
baseColorString, args.get(1), "0", "0");
}
}

/**
* Implementation of the makeMutedColor GSS function. This is intended to
* generate a muted flavor of a text or link color. Takes three arguments: the
Expand Down
Expand Up @@ -244,6 +244,14 @@ public void testDarken() throws GssFunctionException {
function.getCallResultString(ImmutableList.of("#80e619", "20")));
}

public void testSpin() throws GssFunctionException {
GssFunctions.Spin function = new GssFunctions.Spin();
assertEquals("#F2A60D",
function.getCallResultString(ImmutableList.of("#f2330d", "30")));
assertEquals("#F20D5A",
function.getCallResultString(ImmutableList.of("#f2330d", "-30")));
}

/*
* Test that calling the function with the given arguments throws a
* GssFunctionException.
Expand Down

0 comments on commit f209009

Please sign in to comment.