Commit | Line | Data |
---|---|---|
c879c4db AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | ||
10 | package org.lttng.scope.tmf2.views.ui.jfx; | |
11 | ||
12 | import java.util.Map; | |
13 | import java.util.concurrent.ConcurrentHashMap; | |
14 | ||
15 | import org.lttng.scope.tmf2.views.core.timegraph.model.render.ColorDefinition; | |
16 | ||
17 | import javafx.scene.paint.Color; | |
18 | ||
19 | public final class JfxColorFactory { | |
20 | ||
21 | private JfxColorFactory() {} | |
22 | ||
23 | private static final Map<ColorDefinition, Color> COLOR_MAP = new ConcurrentHashMap<>(); | |
24 | private static final Map<ColorDefinition, Color> DERIVED_COLOR_MAP = new ConcurrentHashMap<>(); | |
25 | ||
26 | /** | |
27 | * Instantiate a {@link Color} from a {@link ColorDefinition} object. | |
28 | * | |
29 | * @param colorDef | |
30 | * The ColorDefinition | |
31 | * @return The Color object | |
32 | */ | |
33 | public static synchronized Color getColorFromDef(ColorDefinition colorDef) { | |
34 | Color color = COLOR_MAP.get(colorDef); | |
35 | if (color == null) { | |
36 | color = Color.rgb(colorDef.fRed, colorDef.fGreen, colorDef.fBlue, (double) colorDef.fAlpha / (double) ColorDefinition.MAX); | |
37 | COLOR_MAP.put(colorDef, color); | |
38 | } | |
39 | return color; | |
40 | } | |
41 | ||
42 | public static synchronized Color getDerivedColorFromDef(ColorDefinition colorDef) { | |
43 | Color color = DERIVED_COLOR_MAP.get(colorDef); | |
44 | if (color == null) { | |
45 | color = getColorFromDef(colorDef); | |
46 | color = color.desaturate().darker(); | |
47 | DERIVED_COLOR_MAP.put(colorDef, color); | |
48 | } | |
49 | return color; | |
50 | } | |
51 | ||
52 | /** | |
53 | * Convert a JavaFX {@link Color} to its equivalent {@link ColorDefinition}. | |
54 | * | |
55 | * @param color | |
56 | * The color to convert | |
57 | * @return A corresponding ColorDefinition | |
58 | */ | |
59 | public static ColorDefinition colorToColorDef(Color color) { | |
60 | /* | |
61 | * ColorDefintion works with integer values 0 to 255, but JavaFX colors | |
62 | * works with doubles 0.0 to 0.1 | |
63 | */ | |
64 | int red = (int) Math.round(color.getRed() * 255); | |
65 | int green = (int) Math.round(color.getGreen() * 255); | |
66 | int blue = (int) Math.round(color.getBlue() * 255); | |
67 | int opacity = (int) Math.round(color.getOpacity() * 255); | |
68 | return new ColorDefinition(red, green, blue, opacity); | |
69 | } | |
70 | } |