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;
}
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));
}
Aggregations