1
0
mirror of https://github.com/demodude4u/Factorio-FBSR.git synced 2024-11-27 08:20:57 +02:00

Moved util methods that were incorrectly in FDW package into RenderUtils

This commit is contained in:
Weston Rye (Demod) 2017-06-04 22:42:52 -04:00
parent 85816c9d4c
commit 3ab3577a88
4 changed files with 48 additions and 6 deletions

View File

@ -246,7 +246,7 @@ public class FBSR {
} }
DataPrototype prototype = optProto.get(); DataPrototype prototype = optProto.get();
BufferedImage image = FactorioData.getModImage(prototype.lua().get("icon")); BufferedImage image = FactorioData.getModImage(prototype.lua().get("icon"));
Color color = Utils.getAverageColor(image); Color color = RenderUtils.getAverageColor(image);
// return new Color(color.getRGB() | 0xA0A0A0); // return new Color(color.getRGB() | 0xA0A0A0);
// return color.brighter().brighter(); // return color.brighter().brighter();
float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null); float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
@ -424,7 +424,7 @@ public class FBSR {
Stroke ps = g.getStroke(); Stroke ps = g.getStroke();
g.setStroke( g.setStroke(
new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.setColor(Utils.withAlpha(getItemLogisticColor(table, itemName), g.setColor(RenderUtils.withAlpha(getItemLogisticColor(table, itemName),
255 - 127 / s.size())); 255 - 127 / s.size()));
g.draw(new Line2D.Double(d.right().offset(pos, shift), g.draw(new Line2D.Double(d.right().offset(pos, shift),
d.right().offset(d.offset(pos, 0.5), shift))); d.right().offset(d.offset(pos, 0.5), shift)));

View File

@ -0,0 +1,41 @@
package com.demod.fbsr;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public final class RenderUtils {
public static final BufferedImage EMPTY_IMAGE = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
static {
EMPTY_IMAGE.setRGB(0, 0, 0x00000000);
}
public static Color getAverageColor(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
float sumR = 0, sumG = 0, sumB = 0, sumA = 0;
for (int pixel : pixels) {
float a = (pixel >> 24) & 0xFF;
float f = a / 255;
sumA += a;
sumR += ((pixel >> 16) & 0xFF) * f;
sumG += ((pixel >> 8) & 0xFF) * f;
sumB += ((pixel) & 0xFF) * f;
}
return new Color(sumR / sumA, sumG / sumA, sumB / sumA);
}
public static BufferedImage scaleImage(BufferedImage image, int width, int height) {
BufferedImage ret = new BufferedImage(width, height, image.getType());
Graphics2D g = ret.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return ret;
}
public static Color withAlpha(Color color, int alpha) {
return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
}
private RenderUtils() {
}
}

View File

@ -27,11 +27,11 @@ import javax.imageio.ImageIO;
import com.demod.dcba.CommandHandler; import com.demod.dcba.CommandHandler;
import com.demod.dcba.DCBA; import com.demod.dcba.DCBA;
import com.demod.dcba.DiscordBot; import com.demod.dcba.DiscordBot;
import com.demod.factorio.Utils;
import com.demod.fbsr.Blueprint; import com.demod.fbsr.Blueprint;
import com.demod.fbsr.BlueprintReporting; import com.demod.fbsr.BlueprintReporting;
import com.demod.fbsr.BlueprintStringData; import com.demod.fbsr.BlueprintStringData;
import com.demod.fbsr.FBSR; import com.demod.fbsr.FBSR;
import com.demod.fbsr.RenderUtils;
import com.demod.fbsr.WebUtils; import com.demod.fbsr.WebUtils;
import com.demod.fbsr.WorldMap; import com.demod.fbsr.WorldMap;
import com.demod.fbsr.WorldMap.Debug; import com.demod.fbsr.WorldMap.Debug;
@ -203,7 +203,7 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
} }
if (imageData.length > 8000000) { if (imageData.length > 8000000) {
return generateDiscordFriendlyPNGImage( return generateDiscordFriendlyPNGImage(
Utils.scaleImage(image, image.getWidth() / 2, image.getHeight() / 2)); RenderUtils.scaleImage(image, image.getWidth() / 2, image.getHeight() / 2));
} }
return imageData; return imageData;
} }

View File

@ -35,6 +35,7 @@ import com.demod.factorio.prototype.RecipePrototype;
import com.demod.fbsr.BlueprintEntity; import com.demod.fbsr.BlueprintEntity;
import com.demod.fbsr.Direction; import com.demod.fbsr.Direction;
import com.demod.fbsr.LogisticGridCell; import com.demod.fbsr.LogisticGridCell;
import com.demod.fbsr.RenderUtils;
import com.demod.fbsr.Renderer; import com.demod.fbsr.Renderer;
import com.demod.fbsr.Renderer.Layer; import com.demod.fbsr.Renderer.Layer;
import com.demod.fbsr.WorldMap; import com.demod.fbsr.WorldMap;
@ -73,7 +74,7 @@ public class TypeRendererFactory {
register.accept(new Renderer(Layer.OVERLAY3, entity.getPosition()) { register.accept(new Renderer(Layer.OVERLAY3, entity.getPosition()) {
@Override @Override
public void render(Graphics2D g) { public void render(Graphics2D g) {
g.setColor(Utils.withAlpha( g.setColor(RenderUtils.withAlpha(
Color.getHSBColor(new Random(prototype.getName().hashCode()).nextFloat(), 1f, 1f), 128)); Color.getHSBColor(new Random(prototype.getName().hashCode()).nextFloat(), 1f, 1f), 128));
g.fill(new Rectangle2D.Double(bounds.x - 0.5, bounds.y - 0.5, 1, 1)); g.fill(new Rectangle2D.Double(bounds.x - 0.5, bounds.y - 0.5, 1, 1));
} }
@ -189,7 +190,7 @@ public class TypeRendererFactory {
} }
String blendMode = lua.get("blend_mode").optjstring("normal"); String blendMode = lua.get("blend_mode").optjstring("normal");
if (!blendMode.equals("normal")) { // FIXME blending will take effort if (!blendMode.equals("normal")) { // FIXME blending will take effort
ret.image = Utils.EMPTY_IMAGE; ret.image = RenderUtils.EMPTY_IMAGE;
} }
int srcX = lua.get("x").optint(0); int srcX = lua.get("x").optint(0);
int srcY = lua.get("y").optint(0); int srcY = lua.get("y").optint(0);