Created
May 31, 2019 18:33
-
-
Save rz7d/5beaea5ea8a707634f0b66d8b3a4fbef to your computer and use it in GitHub Desktop.
automatic generator for resources of Void Power mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2019 azure. | |
* | |
* "THE SUSHI-WARE LICENSE" | |
* | |
* <[email protected]> wrote this license. | |
* | |
* As long as you retain this notice you can do whatever you want | |
* with this stuff. If we meet some day, and you think this stuff | |
* is worth it, you can buy me a **sushi 🍣** in return. | |
* | |
* (This license is based on ["THE BEER-WARE LICENSE" (Revision 42)]. | |
* Thanks a lot, Poul-Henning Kamp ;) | |
* | |
* ["THE BEER-WARE LICENSE" (Revision 42)]: https://people.freebsd.org/~phk/ | |
*/ | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.DataBufferInt; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.InputStreamReader; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import javax.imageio.ImageIO; | |
public class AutoSaturationAndBrightness { | |
public static void main(String[] args) throws Exception { | |
var reader = new BufferedReader(new InputStreamReader(System.in)); | |
Path dir = Paths.get(reader.readLine()); | |
for (Path path : (Iterable<Path>) Files | |
.walk(dir) | |
.filter(s -> s.toString().endsWith(".png")) | |
.filter(s -> !s.toString().contains(".edit.")) | |
.filter(Files::isRegularFile)::iterator) { | |
var image = ImageIO.read(path.toFile()); | |
var w = image.getWidth(); | |
var h = image.getHeight(); | |
var out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); | |
var g = out.createGraphics(); | |
g.drawImage(image, 0, 0, w, h, null); | |
g.dispose(); | |
int[] data = ((DataBufferInt) out.getRaster().getDataBuffer()).getData(); | |
int size = data.length; | |
AHSB[] hsb = toHSB(data); | |
for (int i = 0; i < size; ++i) { | |
var p = hsb[i]; | |
if (Float.compare(p.saturation(), 0F) == 0) continue; | |
hsb[i] = new AHSB(p.alpha(), p.hue(), 0, Math.max(0F, p.brightness() - 0.2F)); | |
} | |
System.arraycopy(toRGB(hsb), 0, data, 0, size); | |
File output = new File(path.toString()); | |
output.delete(); | |
ImageIO.write(out, "PNG", output); | |
} | |
} | |
public static AHSB[] toHSB(int[] data) { | |
final var size = data.length; | |
AHSB[] out = new AHSB[size]; | |
for (int i = 0; i < size; ++i) { | |
int color = data[i]; | |
int a = (color & 0xFF000000) >>> 24; | |
int r = (color & 0xFF0000) >>> 16; | |
int g = (color & 0xFF00) >>> 8; | |
int b = color & 0xFF; | |
float[] hsb = new float[3]; | |
Color.RGBtoHSB(r, g, b, hsb); | |
out[i] = new AHSB(a, hsb[0], hsb[1], hsb[2]); | |
} | |
return out; | |
} | |
public static int[] toRGB(AHSB[] data) { | |
final var size = data.length; | |
int[] out = new int[size]; | |
for (int i = 0; i < size; ++i) { | |
var hsb = data[i]; | |
int color = Color.HSBtoRGB(hsb.hue(), hsb.saturation(), hsb.brightness()); | |
out[i] = (color & 0xFFFFFF) | (hsb.alpha() << 24); | |
} | |
return out; | |
} | |
static class AHSB { | |
private final int alpha; | |
private final float hue; | |
private final float saturation; | |
private final float brightness; | |
public AHSB(int alpha, float hue, float saturation, float brightness) { | |
this.alpha = alpha; | |
this.hue = hue; | |
this.saturation = saturation; | |
this.brightness = brightness; | |
} | |
public int alpha() { | |
return alpha; | |
} | |
public float hue() { | |
return hue; | |
} | |
public float saturation() { | |
return saturation; | |
} | |
public float brightness() { | |
return brightness; | |
} | |
} | |
static void printColor(int color) { | |
System.out.printf("#%08x%n", color); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment