Files
ts3java/ts3-client/tools/AppIcon.java
T
2026-10-14 18:34:12 +00:00

181 lines
8.0 KiB
Java

import java.awt.BasicStroke;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
/**
* Draws the application icon: Rubin's vase, whose outline is two faces looking at each
* other, wearing one headset — the left face talks into its microphone. Writes the
* desktop's SVGs and the Android launcher's foreground.
*
* <p>Run from the repository root: {@code java ts3-client/tools/AppIcon.java}.
*/
public class AppIcon {
/** The left face, top to bottom, in a 256 box; the vase is it and its mirror image. */
static final double[][] PROFILE = {
{74, 52}, {90, 72}, {100, 94}, {102, 103}, {100, 109}, {109, 119}, {118, 129},
{115, 135}, {106, 137}, {108, 146}, {103, 152}, {106, 160}, {100, 167}, {104, 176},
{107, 184}, {100, 191}, {92, 197}, {89, 208}, {86, 216}, {74, 224}, {68, 226}};
static final String LIGHT = "4FA3FF";
static final String DARK = "2442B8";
/** Headset geometry; the small icon gets a bolder one. */
record Headset(double earX, double earY, double cupW, double cupH, double band, double bandTop,
double boom, double micX, double micY, double mic, double gap) {
}
static final Headset LARGE = new Headset(40, 124, 30, 46, 13, 28, 9, 90, 156, 8, 6);
static final Headset SMALL = new Headset(36, 124, 40, 58, 22, 22, 15, 88, 160, 12, 8);
public static void main(String[] args) throws IOException {
Path root = Path.of("");
Path ui = root.resolve("ts3-client/swing/src/main/resources/eu/lixko/ts3j/client/ui/icons");
Files.writeString(ui.resolve("app-icon.svg"), svg(path(icon(LARGE), 6)));
Files.writeString(ui.resolve("app-icon-small.svg"), svg(path(icon(SMALL), 0.5)));
Files.writeString(root.resolve("android/app/src/main/res/drawable/ic_launcher_foreground.xml"),
vector(path(icon(LARGE), 6)));
}
/** The vase with the headset, which a gap keeps apart from it. */
static Area icon(Headset h) {
Path2D outline = new Path2D.Double();
outline.moveTo(PROFILE[0][0], PROFILE[0][1]);
spline(outline, PROFILE);
double[][] right = new double[PROFILE.length][];
for (int i = 0; i < PROFILE.length; i++) {
double[] p = PROFILE[PROFILE.length - 1 - i];
right[i] = new double[]{256 - p[0], p[1]};
}
outline.lineTo(right[0][0], right[0][1]);
spline(outline, right);
outline.closePath();
Area set = new Area();
set.add(new Area(cup(h.earX(), h)));
set.add(new Area(cup(256 - h.earX(), h)));
double r = 128 - h.earX(), bandBottom = h.earY() - h.cupH() / 2 + 4;
set.add(stroke(new Arc2D.Double(h.earX(), h.bandTop(), 2 * r, 2 * (bandBottom - h.bandTop()),
0, 180, Arc2D.OPEN), h.band()));
Path2D boom = new Path2D.Double();
boom.moveTo(h.earX() + 4, h.earY() + h.cupH() / 2 - 8);
boom.curveTo(h.earX() + 10, h.micY() + 12, h.micX() - 30, h.micY() + 8, h.micX(), h.micY());
set.add(stroke(boom, h.boom()));
set.add(new Area(new Ellipse2D.Double(h.micX() - h.mic(), h.micY() - h.mic(), 2 * h.mic(), 2 * h.mic())));
Area icon = new Area(outline);
icon.subtract(stroke(set, 2 * h.gap()));
icon.add(set);
return icon;
}
static Shape cup(double x, Headset h) {
return new RoundRectangle2D.Double(x - h.cupW() / 2, h.earY() - h.cupH() / 2, h.cupW(), h.cupH(),
h.cupW() * 0.9, h.cupW() * 0.9);
}
/** Catmull-Rom through the points; the path is already at the first. */
static void spline(Path2D path, double[][] pts) {
for (int i = 0; i < pts.length - 1; i++) {
double[] p0 = pts[Math.max(i - 1, 0)], p1 = pts[i], p2 = pts[i + 1], p3 = pts[Math.min(i + 2, pts.length - 1)];
path.curveTo(p1[0] + (p2[0] - p0[0]) / 6, p1[1] + (p2[1] - p0[1]) / 6,
p2[0] - (p3[0] - p1[0]) / 6, p2[1] - (p3[1] - p1[1]) / 6, p2[0], p2[1]);
}
}
static Area stroke(Shape shape, double width) {
return new Area(new BasicStroke((float) width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)
.createStrokedShape(shape));
}
/** Path data of the shape scaled to fill the 256 box, less a margin. */
static String path(Area shape, double margin) {
Rectangle2D b = shape.getBounds2D();
double k = (256 - 2 * margin) / Math.max(b.getWidth(), b.getHeight());
AffineTransform t = new AffineTransform();
t.translate(128, 128);
t.scale(k, k);
t.translate(-b.getCenterX(), -b.getCenterY());
StringBuilder s = new StringBuilder();
double[] c = new double[6];
for (PathIterator it = shape.getPathIterator(t); !it.isDone(); it.next()) {
int type = it.currentSegment(c);
switch (type) {
case PathIterator.SEG_MOVETO -> s.append('M').append(points(c, 1));
case PathIterator.SEG_LINETO -> s.append('L').append(points(c, 1));
case PathIterator.SEG_QUADTO -> s.append('Q').append(points(c, 2));
case PathIterator.SEG_CUBICTO -> s.append('C').append(points(c, 3));
case PathIterator.SEG_CLOSE -> s.append('Z');
default -> throw new IllegalStateException();
}
}
return s.toString();
}
static String points(double[] c, int n) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i > 0) s.append(' ');
s.append(number(c[2 * i])).append(',').append(number(c[2 * i + 1]));
}
return s.toString();
}
static String number(double v) {
String s = String.format(Locale.ROOT, "%.1f", v);
return s.endsWith(".0") ? s.substring(0, s.length() - 2) : s;
}
static String svg(String path) {
return """
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256">\
<defs><linearGradient id="g" x1="0" y1="0" x2="0.6" y2="1">\
<stop offset="0" stop-color="#%s"/><stop offset="1" stop-color="#%s"/></linearGradient></defs>\
<path fill="url(#g)" d="%s"/></svg>
""".formatted(LIGHT, DARK, path);
}
static String vector(String path) {
return """
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated by ts3-client/tools/AppIcon.java; the art sits in the adaptive icon's safe zone. -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="256"
android:viewportHeight="256">
<group
android:scaleX="0.62"
android:scaleY="0.62"
android:pivotX="128"
android:pivotY="128">
<path android:pathData="%s">
<aapt:attr name="android:fillColor">
<gradient
android:type="linear"
android:startX="0"
android:startY="0"
android:endX="154"
android:endY="256"
android:startColor="#FF%s"
android:endColor="#FF%s" />
</aapt:attr>
</path>
</group>
</vector>
""".formatted(path, LIGHT, DARK);
}
}