ColorUtil: fix modHSV, Color: HSV (#673)

This commit is contained in:
Gustl22
2019-02-23 01:38:33 +01:00
committed by Emux
parent 2efa8808c0
commit 627a316e4d
5 changed files with 183 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2018 Gustl22
* Copyright 2018-2019 Gustl22
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@@ -18,10 +18,45 @@
package org.oscim.backend.canvas;
import org.oscim.utils.ColorUtil;
import org.oscim.utils.FastMath;
import org.oscim.utils.math.Vec3;
public final class Color {
public static class HSV {
public double hue;
public double saturation;
public double value;
public HSV() {
hue = 0;
saturation = 1;
value = 1;
}
public HSV(double hue, double saturation, double value) {
this.hue = hue;
this.saturation = saturation;
this.value = value;
}
public HSV(Vec3 hsv) {
hue = hsv.x;
saturation = hsv.y;
value = hsv.z;
}
public int mod(int color, boolean relative) {
return ColorUtil.modHsv(color, hue, saturation, value, relative);
}
@Override
public String toString() {
return "HSV: " + hue + ", " + saturation + ", " + value;
}
}
private static final int OPAQUE = 0xff000000;
public static int fadePremul(int color, double alpha) {
@@ -262,6 +297,10 @@ public final class Color {
return (color & OPAQUE) == OPAQUE;
}
public static String toString(int color) {
return "RGB: " + Color.r(color) + ", " + Color.g(color) + ", " + Color.b(color);
}
private Color() {
}
}

View File

@@ -212,7 +212,7 @@ public class MeshBucket extends RenderBucket {
int c = (ml.area == null) ? Color.BLUE : ml.area.color;
gl.lineWidth(1);
//c = ColorUtil.shiftHue(c, 0.5);
c = ColorUtil.modHsv(c, 1.1, 1.0, 0.8, true);
c = ColorUtil.modHsv(c, 0.1, 1.0, 0.8, true);
GLUtils.setColor(s.uColor, c, 1);
gl.drawElements(GL.LINES,
ml.numIndices,

View File

@@ -63,19 +63,28 @@ public class ColorUtil {
}
/**
* @param hue the hue
* @param hue the hue from 0 to 1 (exclusive)
* 0: no color shift
* 0.5: opposite hue
* @param saturation the saturation
* @param value the lightness (usual a range from 0 to 2)
* 0 to 1: desaturate
* 1 to 2: saturate
* @param value the lightness
* 0 to 1: darken
* 1 to 2: lighten
* @param relative indicate if colors are modified relative to their values
* (e.g black not changes if relative)
*/
public static synchronized int modHsv(int color, double hue, double saturation, double value,
boolean relative) {
if ((hue == 0 || hue == 1) && saturation == 1 && value == 1)
return color;
Vec3 hsl = TMP_VEC;
rgbToHsv(r(color), g(color), b(color), hsl);
return hsvToRgb(clamp(hue * hsl.x, 0, 1),
clamp(saturation * hsl.y, 0, 1),
clamp(relative || (value - 1) < 0 ? value * hsl.z :
return hsvToRgb(clamp((hue + hsl.x) % 1, 0, 1),
clamp(relative || saturation <= 1 ? saturation * hsl.y :
hsl.y + (saturation - 1) * (1 - hsl.y), 0, 1),
clamp(relative || value <= 1 ? value * hsl.z :
hsl.z + (value - 1) * (1 - hsl.z), 0, 1));
}