plantuml/src/net/sourceforge/plantuml/eggs/PSystemColors.java

273 lines
9.3 KiB
Java
Raw Normal View History

2017-03-12 17:22:02 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2017-03-12 17:22:02 +00:00
*
* Project Info: http://plantuml.com
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2017-03-12 17:22:02 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.eggs;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
2017-06-05 11:27:21 +00:00
import net.sourceforge.plantuml.BackSlash;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.PlainDiagram;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.SpriteContainerEmpty;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
import net.sourceforge.plantuml.awt.geom.XPoint2D;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.core.DiagramDescription;
2021-05-23 15:35:13 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.UDrawable;
import net.sourceforge.plantuml.ugraphic.UFont;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.URectangle;
2022-10-05 20:32:57 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorSet;
import net.sourceforge.plantuml.ugraphic.color.HColorSimple;
2022-08-19 16:34:21 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColors;
2017-03-12 17:22:02 +00:00
// http://www.redblobgames.com/grids/hexagons/
public class PSystemColors extends PlainDiagram implements UDrawable {
2017-03-12 17:22:02 +00:00
private final double rectangleHeight = 28;
private final double rectangleWidth = 175;
2020-03-18 10:50:02 +00:00
private final HColorSet colors = HColorSet.instance();
2017-03-12 17:22:02 +00:00
private final String paletteCentralColor;
private final double size = 60;
2021-05-23 15:35:13 +00:00
public PSystemColors(UmlSource source, String option) {
super(source);
2022-08-23 16:39:27 +00:00
if (option == null)
2017-03-12 17:22:02 +00:00
this.paletteCentralColor = null;
2022-08-23 16:39:27 +00:00
else
2017-03-12 17:22:02 +00:00
this.paletteCentralColor = option.replaceAll("\\#", "");
2022-08-23 16:39:27 +00:00
2017-03-12 17:22:02 +00:00
}
@Override
protected UDrawable getRootDrawable(FileFormatOption fileFormatOption) {
return this;
2017-03-12 17:22:02 +00:00
}
public DiagramDescription getDescription() {
return new DiagramDescription("(Colors)");
}
public void drawU(UGraphic ug) {
2022-08-23 16:39:27 +00:00
if (paletteCentralColor != null && colors.getColorOrWhite(paletteCentralColor) instanceof HColorSimple)
2017-03-12 17:22:02 +00:00
drawPalette(ug);
2022-08-23 16:39:27 +00:00
else
2017-03-21 21:37:59 +00:00
drawFull(ug);
2017-03-12 17:22:02 +00:00
}
private void drawPalette(UGraphic ug) {
double x = (centerHexa(2, 0).getX() + centerHexa(3, 0).getX()) / 2;
double y = centerHexa(0, 2).getY() + corner(1).getY();
ug = ug.apply(new UTranslate(x, y));
final UPolygon hexa = getHexa();
final List<String> friends = getColorsCloseTo(paletteCentralColor);
int idx = 0;
drawOneHexa(ug, friends.get(idx++), 0, 0, hexa);
drawOneHexa(ug, friends.get(idx++), 1, 0, hexa);
drawOneHexa(ug, friends.get(idx++), 0, 1, hexa);
drawOneHexa(ug, friends.get(idx++), -1, 1, hexa);
drawOneHexa(ug, friends.get(idx++), -1, 0, hexa);
drawOneHexa(ug, friends.get(idx++), -1, -1, hexa);
drawOneHexa(ug, friends.get(idx++), 0, -1, hexa);
drawOneHexa(ug, friends.get(idx++), 2, 0, hexa);
drawOneHexa(ug, friends.get(idx++), 1, 1, hexa);
drawOneHexa(ug, friends.get(idx++), 1, 2, hexa);
drawOneHexa(ug, friends.get(idx++), 0, 2, hexa);
drawOneHexa(ug, friends.get(idx++), -1, 2, hexa);
drawOneHexa(ug, friends.get(idx++), -2, 1, hexa);
drawOneHexa(ug, friends.get(idx++), -2, 0, hexa);
drawOneHexa(ug, friends.get(idx++), -2, -1, hexa);
drawOneHexa(ug, friends.get(idx++), -1, -2, hexa);
drawOneHexa(ug, friends.get(idx++), 0, -2, hexa);
drawOneHexa(ug, friends.get(idx++), 1, -2, hexa);
drawOneHexa(ug, friends.get(idx++), 1, -1, hexa);
}
2022-09-12 20:08:34 +00:00
private XPoint2D centerHexa(int i, int j) {
2017-03-12 17:22:02 +00:00
final double width = getWidth();
final double x = width * i + (j % 2 == 0 ? 0 : width / 2);
final double y = size * j * 1.5;
2022-09-12 20:08:34 +00:00
return new XPoint2D(x, y);
2017-03-12 17:22:02 +00:00
}
private double getWidth() {
return Math.sqrt(3) / 2 * 2 * size;
}
private void drawOneHexa(UGraphic ug, String colorName, int i, int j, UPolygon hexa) {
2022-08-23 16:39:27 +00:00
final HColor color = colors.getColorOrWhite(colorName);
2017-03-12 17:22:02 +00:00
ug = applyColor(ug, color);
ug = ug.apply(new UTranslate(centerHexa(i, j)));
ug.draw(hexa);
2017-04-05 17:37:42 +00:00
final UFont font = UFont.sansSerif(14).bold();
2017-03-12 17:22:02 +00:00
TextBlock tt = getTextName(font, colorName, color);
2022-09-12 20:08:34 +00:00
XDimension2D dimText = tt.calculateDimension(ug.getStringBounder());
2017-03-12 17:22:02 +00:00
if (dimText.getWidth() > getWidth()) {
tt = getTextName(font, findShortest(ug.getStringBounder(), font, colorName), color);
dimText = tt.calculateDimension(ug.getStringBounder());
}
tt.drawU(ug.apply(new UTranslate(-dimText.getWidth() / 2, -dimText.getHeight() / 2)));
}
private String findShortest(StringBounder stringBounder, UFont font, String colorName) {
String result = null;
double min = Double.MAX_VALUE;
for (int i = 1; i < colorName.length() - 1; i++) {
2022-08-23 16:39:27 +00:00
if (Character.isLowerCase(colorName.charAt(i)))
2017-03-12 17:22:02 +00:00
continue;
2022-08-23 16:39:27 +00:00
2017-06-05 11:27:21 +00:00
final String candidat = colorName.substring(0, i) + BackSlash.BS_BS_N + colorName.substring(i);
2022-08-19 16:34:21 +00:00
final TextBlock tt = getTextName(font, candidat, (HColorSimple) HColors.BLACK);
2017-03-12 17:22:02 +00:00
final double width = tt.calculateDimension(stringBounder).getWidth();
if (width < min) {
result = candidat;
min = width;
}
}
return result;
}
2020-03-18 10:50:02 +00:00
private UGraphic applyColor(UGraphic ug, HColor color) {
2020-04-19 16:04:39 +00:00
return ug.apply(color).apply(color.bg());
2017-03-12 17:22:02 +00:00
}
2022-09-12 20:08:34 +00:00
private XPoint2D corner(int i) {
2017-03-12 17:22:02 +00:00
double angle_deg = 60 * i + 30;
double angle_rad = Math.PI / 180 * angle_deg;
2022-09-12 20:08:34 +00:00
return new XPoint2D(size * Math.cos(angle_rad), size * Math.sin(angle_rad));
2017-03-12 17:22:02 +00:00
}
private UPolygon getHexa() {
final UPolygon result = new UPolygon();
2022-08-23 16:39:27 +00:00
for (int i = 0; i < 6; i++)
2017-03-12 17:22:02 +00:00
result.addPoint(corner(i));
2022-08-23 16:39:27 +00:00
2017-03-12 17:22:02 +00:00
return result;
}
private List<String> getColorsCloseTo(String other) {
2021-05-14 08:42:57 +00:00
final List<String> result = new ArrayList<>(colors.names());
2017-03-12 17:22:02 +00:00
for (Iterator<String> it = result.iterator(); it.hasNext();) {
final String candidat = it.next();
final String similar = candidat.replaceAll("Gray", "Grey");
2022-08-23 16:39:27 +00:00
if (candidat.equals(similar))
2017-03-12 17:22:02 +00:00
continue;
2022-08-23 16:39:27 +00:00
if (result.contains(similar))
2017-03-12 17:22:02 +00:00
it.remove();
2022-08-23 16:39:27 +00:00
2017-03-12 17:22:02 +00:00
}
2022-08-23 16:39:27 +00:00
if (containsCaseInsensitive(result, other) == false)
2017-03-12 17:22:02 +00:00
result.add(other);
2022-08-23 16:39:27 +00:00
2017-03-12 17:22:02 +00:00
Collections.sort(result, closeComparator(paletteCentralColor));
return result;
}
private boolean containsCaseInsensitive(Collection<String> source, String target) {
2022-08-23 16:39:27 +00:00
for (String s : source)
if (s.equalsIgnoreCase(target))
2017-03-12 17:22:02 +00:00
return true;
2022-08-23 16:39:27 +00:00
2017-03-12 17:22:02 +00:00
return false;
}
private Comparator<String> closeComparator(String center) {
2021-02-02 10:12:15 +00:00
final HColorSimple centerColor = (HColorSimple) colors.getColorOrWhite(center);
2017-03-12 17:22:02 +00:00
return new Comparator<String>() {
public int compare(String col1, String col2) {
2022-08-23 16:39:27 +00:00
final int dist1 = centerColor.distanceTo((HColorSimple) colors.getColorOrWhite(col1));
final int dist2 = centerColor.distanceTo((HColorSimple) colors.getColorOrWhite(col2));
2017-03-12 17:22:02 +00:00
return (int) Math.signum(dist1 - dist2);
}
};
}
private void drawFull(UGraphic ug) {
2017-04-05 17:37:42 +00:00
final UFont font = UFont.sansSerif(14).bold();
2017-03-12 17:22:02 +00:00
2022-08-19 16:34:21 +00:00
ug = ug.apply(HColors.BLACK);
2017-03-12 17:22:02 +00:00
int i = 0;
int j = 0;
for (String name : colors.names()) {
UGraphic tmp = getPositioned(ug, i, j);
2022-08-23 16:39:27 +00:00
final HColor color = colors.getColorOrWhite(name);
2017-03-12 17:22:02 +00:00
applyColor(tmp, color).draw(new URectangle(rectangleWidth, rectangleHeight));
final TextBlock tt = getTextName(font, name, color);
2022-09-12 20:08:34 +00:00
final XDimension2D dimText = tt.calculateDimension(ug.getStringBounder());
2017-03-12 17:22:02 +00:00
final double dy = (rectangleHeight - dimText.getHeight()) / 2;
final double dx = (rectangleWidth - dimText.getWidth()) / 2;
tt.drawU(tmp.apply(new UTranslate(dx, dy)));
if (j++ == 20) {
j = 0;
i++;
}
}
}
2022-08-23 16:39:27 +00:00
private TextBlock getTextName(final UFont font, String name, final HColor color) {
final HColor opposite = color.opposite();
2022-10-05 20:32:57 +00:00
final FontConfiguration fc = FontConfiguration.create(font, opposite, HColors.BLUE, new UStroke());
2017-03-12 17:22:02 +00:00
final TextBlock tt = Display.getWithNewlines(name).create(fc, HorizontalAlignment.CENTER,
new SpriteContainerEmpty());
return tt;
}
private UGraphic getPositioned(UGraphic ug, int i, int j) {
return ug.apply(new UTranslate(rectangleWidth * i, rectangleHeight * j));
}
}