View Javadoc
1   package org.opentrafficsim.editor;
2   
3   import java.awt.Color;
4   
5   import javax.swing.JLabel;
6   
7   import org.opentrafficsim.draw.ColorInterpolator;
8   import org.opentrafficsim.swing.gui.AppearanceControl;
9   
10  /**
11   * Status label at bottom of the screen with font color interpolated between background and foreground colors in GUI appearance.
12   * <p>
13   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public class StatusLabel extends JLabel implements AppearanceControl
21  {
22  
23      /** */
24      private static final long serialVersionUID = 20231017L;
25  
26      /** Standard background color in appearance. */
27      private Color appearanceBackgroundColor;
28  
29      /** Standard foreground color in appearance. */
30      private Color appearanceForegroundColor;
31  
32      /**
33       * Constructor.
34       */
35      public StatusLabel()
36      {
37          //
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public boolean isBackground()
43      {
44          return true;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public boolean isForeground()
50      {
51          return true;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public boolean isFont()
57      {
58          return true;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public void setBackground(final Color bg)
64      {
65          this.appearanceBackgroundColor = bg;
66          super.setBackground(bg);
67          updateForgroundColor();
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void setForeground(final Color fg)
73      {
74          this.appearanceForegroundColor = fg;
75          super.setForeground(fg);
76          updateForgroundColor();
77      }
78  
79      /**
80       * Updates the foreground color by reducing the contrast with the background color.
81       */
82      private void updateForgroundColor()
83      {
84          if (this.appearanceForegroundColor != null && this.appearanceBackgroundColor != null)
85          {
86              super.setForeground(
87                      ColorInterpolator.interpolateColor(this.appearanceForegroundColor, this.appearanceBackgroundColor, 0.7));
88          }
89      }
90  
91  }