Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | /* |
2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). | |
3 | * | |
4 | * lanterna is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU Lesser General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | * | |
17 | * Copyright (C) 2010-2015 Martin | |
18 | */ | |
19 | package com.googlecode.lanterna.terminal.swing; | |
20 | ||
21 | import com.googlecode.lanterna.TextColor; | |
22 | import java.awt.Color; | |
23 | ||
24 | /** | |
25 | * Color configuration settings to be using with SwingTerminal. This class contains color-related settings that is used | |
26 | * by SwingTerminal when it renders the component. | |
27 | * @author martin | |
28 | */ | |
29 | public class TerminalEmulatorColorConfiguration { | |
30 | ||
31 | /** | |
32 | * This is the default settings that is used when you create a new SwingTerminal without specifying any color | |
33 | * configuration. It will use classic VGA colors for the ANSI palette and bright colors on bold text. | |
34 | */ | |
35 | public static TerminalEmulatorColorConfiguration getDefault() { | |
36 | return newInstance(TerminalEmulatorPalette.STANDARD_VGA); | |
37 | } | |
38 | ||
39 | /** | |
40 | * Creates a new color configuration based on a particular palette and with using brighter colors on bold text. | |
41 | * @param colorPalette Palette to use for this color configuration | |
42 | * @return The resulting color configuration | |
43 | */ | |
44 | @SuppressWarnings("SameParameterValue") | |
45 | public static TerminalEmulatorColorConfiguration newInstance(TerminalEmulatorPalette colorPalette) { | |
46 | return new TerminalEmulatorColorConfiguration(colorPalette, true); | |
47 | } | |
48 | ||
49 | private final TerminalEmulatorPalette colorPalette; | |
50 | private final boolean useBrightColorsOnBold; | |
51 | ||
52 | private TerminalEmulatorColorConfiguration(TerminalEmulatorPalette colorPalette, boolean useBrightColorsOnBold) { | |
53 | this.colorPalette = colorPalette; | |
54 | this.useBrightColorsOnBold = useBrightColorsOnBold; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Given a TextColor and a hint as to if the color is to be used as foreground or not and if we currently have | |
59 | * bold text enabled or not, it returns the closest AWT color that matches this. | |
60 | * @param color What text color to convert | |
61 | * @param isForeground Is the color intended to be used as foreground color | |
62 | * @param inBoldContext Is the color intended to be used for on a character this is bold | |
63 | * @return The AWT color that represents this text color | |
64 | */ | |
65 | public Color toAWTColor(TextColor color, boolean isForeground, boolean inBoldContext) { | |
66 | if(color instanceof TextColor.ANSI) { | |
67 | return colorPalette.get((TextColor.ANSI)color, isForeground, inBoldContext && useBrightColorsOnBold); | |
68 | } | |
69 | return color.toColor(); | |
70 | } | |
71 | } |