-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEOT-5713] Add support for lesscss color operation functions
- Loading branch information
Showing
23 changed files
with
1,290 additions
and
8 deletions.
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
66 changes: 66 additions & 0 deletions
66
...es/library/main/src/main/java/org/geotools/filter/function/color/AbstractHSLFunction.java
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,66 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2017, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.filter.function.color; | ||
|
||
import static org.geotools.filter.capability.FunctionNameImpl.parameter; | ||
|
||
import java.awt.Color; | ||
|
||
import org.geotools.filter.FunctionImpl; | ||
import org.geotools.filter.capability.FunctionNameImpl; | ||
import org.opengis.filter.capability.FunctionName; | ||
|
||
/** | ||
* Base for lesscss.org HSL color manipulation functions | ||
* | ||
* @author Andrea Aime - GeoSolutions | ||
*/ | ||
public abstract class AbstractHSLFunction extends FunctionImpl { | ||
|
||
enum Method { | ||
absolute, relative | ||
}; | ||
|
||
public static FunctionName NAME = new FunctionNameImpl("abstractHSL", | ||
parameter("result", Color.class), parameter("color", Color.class), | ||
parameter("amount", Float.class), parameter("method", Method.class, 0, 1)); | ||
|
||
public AbstractHSLFunction(String name) { | ||
this.functionName = new FunctionNameImpl(name, NAME.getReturn(), NAME.getArguments()); | ||
} | ||
|
||
@Override | ||
public Object evaluate(Object object) { | ||
Color source = (Color) getParameterValue(object, 0); | ||
float amount = (Float) getParameterValue(object, 1); | ||
Method method = (Method) getParameterValue(object, 2, Method.absolute); | ||
|
||
HSLColor hsl = new HSLColor(source); | ||
if (method == Method.absolute) { | ||
adjustAbsolute(amount, hsl); | ||
} else { | ||
adjstRelative(amount, hsl); | ||
} | ||
|
||
return hsl.toRGB(); | ||
} | ||
|
||
protected abstract void adjstRelative(float amount, HSLColor hsl); | ||
|
||
protected abstract void adjustAbsolute(float amount, HSLColor hsl); | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
modules/library/main/src/main/java/org/geotools/filter/function/color/ConstrastFunction.java
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,80 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2017, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.filter.function.color; | ||
|
||
import static org.geotools.filter.capability.FunctionNameImpl.parameter; | ||
|
||
import java.awt.Color; | ||
|
||
import org.geotools.filter.FunctionImpl; | ||
import org.geotools.filter.capability.FunctionNameImpl; | ||
import org.opengis.filter.capability.FunctionName; | ||
|
||
/** | ||
* Contrast lesscss.org color function. Returns the color with highest contrast with a given reference | ||
* | ||
* @author Andrea Aime - GeoSolutions | ||
*/ | ||
public class ConstrastFunction extends FunctionImpl { | ||
|
||
public static FunctionName NAME = new FunctionNameImpl("contrast", | ||
parameter("result", Color.class), parameter("reference", Color.class), | ||
parameter("color1", Color.class, 0, 1), parameter("color2", Color.class, 0, 1), | ||
parameter("threshold", Double.class, 0, 1)); | ||
|
||
public ConstrastFunction() { | ||
this.functionName = NAME; | ||
} | ||
|
||
@Override | ||
public Object evaluate(Object object) { | ||
Color reference = (Color) getParameterValue(object, 0); | ||
Color color1 = (Color) getParameterValue(object, 1, Color.BLACK); | ||
Color color2 = (Color) getParameterValue(object, 2, Color.WHITE); | ||
double threshold = (Double) getParameterValue(object, 3, 0.43); | ||
|
||
double luma1 = luma(color1); | ||
double luma2 = luma(color2); | ||
Color light, dark; | ||
if (luma1 > luma2) { | ||
light = color1; | ||
dark = color2; | ||
} else { | ||
light = color2; | ||
dark = color1; | ||
} | ||
|
||
if (luma(reference) < threshold) { | ||
return light; | ||
} else { | ||
return dark; | ||
} | ||
} | ||
|
||
private double luma(Color color) { | ||
double r = color.getRed() / 255d; | ||
double g = color.getGreen() / 255d; | ||
double b = color.getBlue() / 255d; | ||
|
||
r = (r <= 0.03928) ? r / 12.92 : Math.pow(((r + 0.055) / 1.055), 2.4); | ||
g = (g <= 0.03928) ? g / 12.92 : Math.pow(((g + 0.055) / 1.055), 2.4); | ||
b = (b <= 0.03928) ? b / 12.92 : Math.pow(((b + 0.055) / 1.055), 2.4); | ||
|
||
return 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
modules/library/main/src/main/java/org/geotools/filter/function/color/DarkenFunction.java
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,38 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2017, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.filter.function.color; | ||
|
||
/** | ||
* Implements the lesscss.org darken function | ||
* | ||
* @author Andrea Aime - GeoSolutions | ||
*/ | ||
public class DarkenFunction extends AbstractHSLFunction { | ||
|
||
public DarkenFunction() { | ||
super("darken"); | ||
} | ||
|
||
protected void adjstRelative(float amount, HSLColor hsl) { | ||
hsl.setLightness(hsl.getLightness() * (1 - amount)); | ||
} | ||
|
||
protected void adjustAbsolute(float amount, HSLColor hsl) { | ||
hsl.setLightness(hsl.getLightness() - amount); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...les/library/main/src/main/java/org/geotools/filter/function/color/DesaturateFunction.java
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,38 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2017, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.filter.function.color; | ||
|
||
/** | ||
* Implements the lesscss.org desaturate function | ||
* | ||
* @author Andrea Aime - GeoSolutions | ||
*/ | ||
public class DesaturateFunction extends AbstractHSLFunction { | ||
|
||
public DesaturateFunction() { | ||
super("desaturate"); | ||
} | ||
|
||
protected void adjstRelative(float amount, HSLColor hsl) { | ||
hsl.setSaturation(hsl.getSaturation() * (1 - amount)); | ||
} | ||
|
||
protected void adjustAbsolute(float amount, HSLColor hsl) { | ||
hsl.setSaturation(hsl.getSaturation() - amount); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
modules/library/main/src/main/java/org/geotools/filter/function/color/GrayscaleFunction.java
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,50 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2017, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.filter.function.color; | ||
|
||
import static org.geotools.filter.capability.FunctionNameImpl.parameter; | ||
|
||
import java.awt.Color; | ||
|
||
import org.geotools.filter.FunctionImpl; | ||
import org.geotools.filter.capability.FunctionNameImpl; | ||
import org.opengis.filter.capability.FunctionName; | ||
|
||
/** | ||
* Tint lesscss.org color function. Takes one colors and mixes it with white based on a weight (and their eventual alpha) | ||
* | ||
* @author Andrea Aime - GeoSolutions | ||
*/ | ||
public class GrayscaleFunction extends FunctionImpl { | ||
|
||
public static FunctionName NAME = new FunctionNameImpl("grayscale", | ||
parameter("result", Color.class), parameter("color", Color.class)); | ||
|
||
public GrayscaleFunction() { | ||
this.functionName = NAME; | ||
} | ||
|
||
@Override | ||
public Object evaluate(Object object) { | ||
Color color = (Color) getParameterValue(object, 0); | ||
|
||
HSLColor hsl = new HSLColor(color); | ||
hsl.setSaturation(0); | ||
return hsl.toRGB(); | ||
} | ||
|
||
} |
Oops, something went wrong.