Refactor color parse method to remove barely used ConfigUtils class

This commit is contained in:
Lukas Rieger (Blue) 2022-10-14 10:00:51 +02:00
parent 6bcda783aa
commit 4534202c75
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
5 changed files with 27 additions and 59 deletions

View File

@ -6,7 +6,6 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import de.bluecolored.bluemap.core.map.lowres.LowresTileManager;
import de.bluecolored.bluemap.core.util.ConfigUtils;
import de.bluecolored.bluemap.core.util.math.Color;
import java.lang.reflect.Type;
@ -48,7 +47,7 @@ public class MapSettingsSerializer implements JsonSerializer<BmMap> {
root.add("startPos", context.serialize(startPos));
// skyColor
Color skyColor = new Color().set(ConfigUtils.parseColorFromString(map.getMapSettings().getSkyColor()));
Color skyColor = new Color().parse(map.getMapSettings().getSkyColor());
root.add("skyColor", context.serialize(skyColor));
// ambientLight

View File

@ -27,7 +27,6 @@ package de.bluecolored.bluemap.core.resources;
import com.flowpowered.math.GenericMath;
import com.google.gson.stream.JsonReader;
import de.bluecolored.bluemap.api.debug.DebugDump;
import de.bluecolored.bluemap.core.util.ConfigUtils;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.world.Biome;
import de.bluecolored.bluemap.core.world.BlockNeighborhood;
@ -79,7 +78,7 @@ public class BlockColorCalculatorFactory {
break;
default:
final Color color = new Color();
color.set(ConfigUtils.parseColorFromString(value)).premultiplied();
color.parse(value).premultiplied();
colorFunction = (calculator, block, target) -> target.set(color);
break;
}

View File

@ -4,7 +4,6 @@ import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import de.bluecolored.bluemap.core.util.ConfigUtils;
import de.bluecolored.bluemap.core.util.math.Color;
import java.io.IOException;
@ -55,7 +54,7 @@ public class ColorAdapter extends TypeAdapter<Color> {
in.endObject();
break;
case STRING:
value.set(ConfigUtils.parseColorFromString(in.nextString()));
value.parse(in.nextString());
break;
case NUMBER:
int color = in.nextInt();

View File

@ -1,53 +0,0 @@
/*
* 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.
*/
package de.bluecolored.bluemap.core.util;
public class ConfigUtils {
private ConfigUtils(){}
/**
* Returns a color-integer. 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 static int parseColorFromString(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";
if (val.length() != 8) throw new NumberFormatException("Invalid color format!");
val = val.substring(6, 8) + val.substring(0, 6); // move alpha to front
return Integer.parseUnsignedInt(val, 16);
}
int color = Integer.parseInt(val);
if ((color & 0xFF000000) == 0) color |= 0xFF000000; // assume full alpha if not present
return color;
}
}

View File

@ -26,6 +26,7 @@ package de.bluecolored.bluemap.core.util.math;
import de.bluecolored.bluemap.api.debug.DebugDump;
@SuppressWarnings("UnusedReturnValue")
@DebugDump
public class Color {
@ -162,6 +163,29 @@ public class Color {
return this;
}
/**
* 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";
if (val.length() != 8) throw new NumberFormatException("Invalid color format!");
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);
}
@Override
public String toString() {
return "Color{" +