2021-07-25 13:08:40 +02:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2021-07-17 16:06:50 +02:00
|
|
|
package de.bluecolored.bluemap.core.util.math;
|
|
|
|
|
2022-07-24 12:10:00 +02:00
|
|
|
import de.bluecolored.bluemap.api.debug.DebugDump;
|
2021-11-20 12:31:18 +01:00
|
|
|
|
2022-10-14 10:00:51 +02:00
|
|
|
@SuppressWarnings("UnusedReturnValue")
|
2021-11-20 12:31:18 +01:00
|
|
|
@DebugDump
|
2021-07-17 16:06:50 +02:00
|
|
|
public class Color {
|
|
|
|
|
|
|
|
public float r, g, b, a;
|
|
|
|
public boolean premultiplied;
|
|
|
|
|
|
|
|
public Color set(float r, float g, float b, float a, boolean premultiplied) {
|
|
|
|
this.r = r;
|
|
|
|
this.g = g;
|
|
|
|
this.b = b;
|
|
|
|
this.a = a;
|
|
|
|
this.premultiplied = premultiplied;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color set(Color color) {
|
|
|
|
this.r = color.r;
|
|
|
|
this.g = color.g;
|
|
|
|
this.b = color.b;
|
|
|
|
this.a = color.a;
|
|
|
|
this.premultiplied = color.premultiplied;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color set(int color) {
|
2022-08-07 20:32:55 +02:00
|
|
|
return set(color, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color set(int color, boolean premultiplied) {
|
2021-07-17 16:06:50 +02:00
|
|
|
this.r = ((color >> 16) & 0xFF) / 255f;
|
|
|
|
this.g = ((color >> 8) & 0xFF) / 255f;
|
|
|
|
this.b = (color & 0xFF) / 255f;
|
|
|
|
this.a = ((color >> 24) & 0xFF) / 255f;
|
2022-08-07 20:32:55 +02:00
|
|
|
this.premultiplied = premultiplied;
|
2021-07-17 16:06:50 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:32:55 +02:00
|
|
|
public int getInt() {
|
|
|
|
int r = (int) (this.r * 255) & 0xFF;
|
|
|
|
int g = (int) (this.g * 255) & 0xFF;
|
|
|
|
int b = (int) (this.b * 255) & 0xFF;
|
|
|
|
int a = (int) (this.a * 255) & 0xFF;
|
|
|
|
return (a << 24) | (r << 16) | (g << 8) | b;
|
|
|
|
}
|
|
|
|
|
2021-07-17 16:06:50 +02:00
|
|
|
public Color add(Color color) {
|
|
|
|
if (color.a < 1f && !color.premultiplied){
|
|
|
|
throw new IllegalArgumentException("Can only add premultiplied colors with alpha!");
|
|
|
|
}
|
|
|
|
|
|
|
|
premultiplied();
|
|
|
|
|
|
|
|
this.r += color.r;
|
|
|
|
this.g += color.g;
|
|
|
|
this.b += color.b;
|
|
|
|
this.a += color.a;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:32:55 +02:00
|
|
|
public Color div(int divisor) {
|
|
|
|
premultiplied();
|
|
|
|
|
|
|
|
float p = 1f / divisor;
|
|
|
|
this.r *= p;
|
|
|
|
this.g *= p;
|
|
|
|
this.b *= p;
|
|
|
|
this.a *= p;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-07-17 16:06:50 +02:00
|
|
|
public Color multiply(Color color) {
|
|
|
|
if (color.premultiplied) premultiplied();
|
|
|
|
else straight();
|
|
|
|
|
|
|
|
this.r *= color.r;
|
|
|
|
this.g *= color.g;
|
|
|
|
this.b *= color.b;
|
|
|
|
this.a *= color.a;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color overlay(Color color) {
|
|
|
|
if (color.a < 1f && !color.premultiplied) throw new IllegalArgumentException("Can only overlay premultiplied colors with alpha!");
|
|
|
|
|
|
|
|
premultiplied();
|
|
|
|
|
|
|
|
float p = 1 - color.a;
|
|
|
|
this.a = p * this.a + color.a;
|
|
|
|
this.r = p * this.r + color.r;
|
|
|
|
this.g = p * this.g + color.g;
|
|
|
|
this.b = p * this.b + color.b;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color flatten() {
|
|
|
|
if (this.a == 1f) return this;
|
|
|
|
|
2022-08-07 20:32:55 +02:00
|
|
|
if (premultiplied && this.a > 0f) {
|
2022-05-28 21:55:41 +02:00
|
|
|
float m = 1f / this.a;
|
|
|
|
this.r *= m;
|
|
|
|
this.g *= m;
|
|
|
|
this.b *= m;
|
2021-07-17 16:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.a = 1f;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color premultiplied() {
|
|
|
|
if (!premultiplied) {
|
|
|
|
this.r *= this.a;
|
|
|
|
this.g *= this.a;
|
|
|
|
this.b *= this.a;
|
|
|
|
this.premultiplied = true;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color straight() {
|
|
|
|
if (premultiplied) {
|
2022-08-07 20:32:55 +02:00
|
|
|
if (this.a > 0f) {
|
|
|
|
float m = 1f / this.a;
|
|
|
|
this.r *= m;
|
|
|
|
this.g *= m;
|
|
|
|
this.b *= m;
|
|
|
|
}
|
2021-07-17 16:06:50 +02:00
|
|
|
this.premultiplied = false;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:00:51 +02:00
|
|
|
/**
|
|
|
|
* Parses the color from a string and sets it to this Color instance.
|
|
|
|
* The value can be an integer in String-Format or a string in hexadecimal format prefixed with # (css-style: e.g. #f16 becomes #ff1166).
|
|
|
|
* @param val The String to parse
|
|
|
|
* @return The parsed Integer
|
|
|
|
* @throws NumberFormatException If the value is not formatted correctly or if there is no value present.
|
|
|
|
*/
|
|
|
|
public Color parse(String val) {
|
|
|
|
if (val.charAt(0) == '#') {
|
|
|
|
val = val.substring(1);
|
|
|
|
if (val.length() == 3) val = val + "f";
|
|
|
|
if (val.length() == 4) val = "" + val.charAt(0) + val.charAt(0) + val.charAt(1) + val.charAt(1) + val.charAt(2) + val.charAt(2) + val.charAt(3) + val.charAt(3);
|
|
|
|
if (val.length() == 6) val = val + "ff";
|
2023-02-07 11:03:54 +01:00
|
|
|
if (val.length() != 8) throw new NumberFormatException("Invalid color format: '" + val + "'!");
|
2022-10-14 10:00:51 +02:00
|
|
|
val = val.substring(6, 8) + val.substring(0, 6); // move alpha to front
|
|
|
|
return set(Integer.parseUnsignedInt(val, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
int color = Integer.parseInt(val);
|
|
|
|
if ((color & 0xFF000000) == 0) color |= 0xFF000000; // assume full alpha if not present
|
|
|
|
return set(color);
|
|
|
|
}
|
|
|
|
|
2021-07-17 16:06:50 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Color{" +
|
2022-08-08 21:29:16 +02:00
|
|
|
"r=" + r + " (" + (int) (r * 255) + ")" +
|
|
|
|
", g=" + g + " (" + (int) (g * 255) + ")" +
|
|
|
|
", b=" + b + " (" + (int) (b * 255) + ")" +
|
|
|
|
", a=" + a + " (" + (int) (a * 255) + ")" +
|
2021-07-17 16:06:50 +02:00
|
|
|
", premultiplied=" + premultiplied +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|