GeometryUtils: length, normalize | ColorUtil: blend (#653)

This commit is contained in:
Gustl22 2019-02-08 14:10:10 +01:00 committed by Emux
parent 0a8e56ea05
commit 28b16a3232
No known key found for this signature in database
GPG Key ID: 64ED9980896038C3
2 changed files with 54 additions and 1 deletions

View File

@ -1,3 +1,20 @@
/*
* Copyright 2014 Hannes Janetzek
* Copyright 2019 Gustl22
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program 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, either version 3 of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.utils;
import org.oscim.backend.canvas.Color;
@ -261,4 +278,22 @@ public class ColorUtil {
public static int hslToRgb(double h, double s, double l) {
return hslToRgb(h, s, l, null);
}
/**
* Blend two colors.
*
* @param color1 the first color
* @param color2 the second color
* @param mix the mixing proportion in range 0 to 1
* @return the blended color
*/
public static int blend(int color1, int color2, float mix) {
float mix2 = 1f - mix;
return Color.get(
(int) ((((color2 >>> 24) & 0xff) * mix) + (((color1 >>> 24) & 0xff) * mix2)),
(int) ((((color2 >>> 16) & 0xff) * mix) + (((color1 >>> 16) & 0xff) * mix2)),
(int) ((((color2 >>> 8) & 0xff) * mix) + (((color1 >>> 8) & 0xff) * mix2)),
(int) ((((color2 >>> 0) & 0xff) * mix) + (((color1 >>> 0) & 0xff) * mix2))
);
}
}

View File

@ -1,6 +1,6 @@
/*
* Copyright 2012, 2013 Hannes Janetzek
* Copyright 2018 Gustl22
* Copyright 2018-2019 Gustl22
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -340,6 +340,24 @@ public final class GeometryUtils {
return (pB[1] - pA[1]) * (pC[0] - pA[0]) - (pB[0] - pA[0]) * (pC[1] - pA[1]);
}
/**
* @return the length of the vector
*/
public static double length(float[] vec) {
float length = 0f;
for (float coord : vec) {
length += coord * coord;
}
return Math.sqrt(length);
}
/**
* @return the normalized vector (with length 1)
*/
public static float[] normalize(float[] vec) {
return scale(vec, 1f / (float) length(vec));
}
/**
* Calculate the normalized direction vectors of point list (polygon)
*