Search in sources :

Example 1 with SpongeMapColor

use of org.spongepowered.common.map.color.SpongeMapColor in project SpongeCommon by SpongePowered.

the class SpongeMapCanvasBuilder method fromImage.

@Override
public MapCanvas.Builder fromImage(final Image image) {
    Objects.requireNonNull(image, "image cannot be null");
    if (image.getWidth(null) != Constants.Map.MAP_PIXELS || image.getHeight(null) != Constants.Map.MAP_PIXELS) {
        throw new IllegalArgumentException("image size was invalid!");
    }
    final BufferedImage bufferedImage = this.createBufferedImage(image);
    final int[] pixels = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()).getData();
    // Get the color palette we are working with
    final Map<Integer, SpongeMapColor> palette = new HashMap<>();
    Sponge.game().registry(RegistryTypes.MAP_COLOR_TYPE).stream().forEach(color -> {
        Sponge.game().registry(RegistryTypes.MAP_SHADE).stream().forEach(shade -> {
            final SpongeMapColor spongeMapColor = new SpongeMapColor(color, shade);
            palette.put(spongeMapColor.color().rgb(), spongeMapColor);
        });
    });
    final byte[] canvas = this.getCanvas();
    for (int i = 0; i < pixels.length; i++) {
        final SpongeMapColor color = palette.get(pixels[i]);
        if (color == null) {
            throw new IllegalArgumentException("Can not find a matching color for rgb value: " + Integer.toHexString(pixels[i]) + ". The MapCanvas will have painted all pixels up to this point.");
        }
        canvas[i] = color.getMCColor();
    }
    return this;
}
Also used : HashMap(java.util.HashMap) DataBufferInt(java.awt.image.DataBufferInt) SpongeMapColor(org.spongepowered.common.map.color.SpongeMapColor) BufferedImage(java.awt.image.BufferedImage)

Example 2 with SpongeMapColor

use of org.spongepowered.common.map.color.SpongeMapColor in project SpongeCommon by SpongePowered.

the class MapUtil method getMapColorFromPixelValue.

public static Optional<MapColor> getMapColorFromPixelValue(final byte value) {
    final int intColor = Byte.toUnsignedInt(value);
    final int shade = intColor % Constants.Map.MAP_SHADES;
    final int colorIndex = (intColor - shade) / Constants.Map.MAP_SHADES;
    final Optional<MapColorType> mapColorType = MapUtil.getMapColorTypeByColorIndex(colorIndex);
    final MapShade mapShade = MapUtil.getMapShadeById(shade);
    if (!mapColorType.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new SpongeMapColor(mapColorType.get(), mapShade));
}
Also used : MapShade(org.spongepowered.api.map.color.MapShade) SpongeMapColor(org.spongepowered.common.map.color.SpongeMapColor) MapColorType(org.spongepowered.api.map.color.MapColorType)

Aggregations

SpongeMapColor (org.spongepowered.common.map.color.SpongeMapColor)2 BufferedImage (java.awt.image.BufferedImage)1 DataBufferInt (java.awt.image.DataBufferInt)1 HashMap (java.util.HashMap)1 MapColorType (org.spongepowered.api.map.color.MapColorType)1 MapShade (org.spongepowered.api.map.color.MapShade)1