<commit_before>/*
 * $Id$
 *
 * Copyright (c) 2008 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASSAL.tools;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.UIManager;

/**
 * A {@link JButton} which displays a color swatch.
 *
 * @author <NAME>
 * @since 3.1.0
 */
public class ColorButton extends JButton {
  private static final long serialVersionUID = 1L;

  private static Font FONT = new Font("Dialog", 0, 10);

  private Color color;

  /**
   * Create a button with no color.
   */
  public ColorButton() {
    this(null);
  }

 /**
  * Create a button with the specified color.
  *
  * @param c the color to set
  */
  public ColorButton(Color c) {
    super();
    color = c;
    setIcon(new SwatchIcon(30,15));
    setMargin(new Insets(2,2,2,2));
  }

  /**
   * Set the color of the button.
   *
   * @param c the color to set
   */
  public void setColor(Color c) {
    color = c;
  }

  private class SwatchIcon implements Icon {
    private final int swatchWidth;
    private final int swatchHeight;

    public SwatchIcon(int width, int height) {
      swatchWidth = width;
      swatchHeight = height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
      g.setColor(Color.black);
      g.drawRect(x, y, swatchWidth-1, swatchHeight-1);

      if (color != null) {
        g.setColor(color);
        g.fillRect(x+1, y+1, swatchWidth-2, swatchHeight-2);
      }
      else {
        // paint no color and a "nil" if the color is null
        g.setColor(UIManager.getColor("controlText"));
        g.setFont(FONT);
        g.drawString("nil",
          x+(swatchWidth - g.getFontMetrics(FONT).stringWidth("nil"))/ 2,
          y+(swatchHeight + g.getFontMetrics(FONT).getAscent())/2);
      }
    }

    public int getIconWidth() {
      return swatchWidth;
    }

    public int getIconHeight() {
      return swatchHeight;
    }
  }
}<commit_msg>Adjusted ColorButton for HiDPI support.<commit_after>/*
 * $Id$
 *
 * Copyright (c) 2008 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASSAL.tools;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.AffineTransform;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.UIManager;

import VASSAL.tools.swing.SwingUtils;

/**
 * A {@link JButton} which displays a color swatch.
 *
 * @author <NAME>
 * @since 3.1.0
 */
public class ColorButton extends JButton {
  private static final long serialVersionUID = 1L;

  private static final double os_scale = SwingUtils.getSystemScaling();
  private static final Font FONT = new Font("Dialog", 0, (int)(10 * os_scale));

  private Color color;

  /**
   * Create a button with no color.
   */
  public ColorButton() {
    this(null);
  }

 /**
  * Create a button with the specified color.
  *
  * @param c the color to set
  */
  public ColorButton(Color c) {
    super();
    color = c;
    setIcon(new SwatchIcon(30,15));
    setMargin(new Insets(2,2,2,2));
  }

  /**
   * Set the color of the button.
   *
   * @param c the color to set
   */
  public void setColor(Color c) {
    color = c;
  }

  private class SwatchIcon implements Icon {
    private final int swatchWidth;
    private final int swatchHeight;

    public SwatchIcon(int width, int height) {
      swatchWidth = width;
      swatchHeight = height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
      final Graphics2D g2d = (Graphics2D) g;

      final AffineTransform orig_t = g2d.getTransform();
      g2d.setTransform(SwingUtils.descaleTransform(orig_t));

      x *= os_scale;
      y *= os_scale;
      final int w = (int)(swatchWidth * os_scale);
      final int h = (int)(swatchHeight * os_scale);

      g.setColor(Color.black);
      g.drawRect(x, y, w-1, h-1);

      if (color != null) {
        g.setColor(color);
        g.fillRect(x+1, y+1, w-2, h-2);
      }
      else {
        // paint no color and a "nil" if the color is null
        g.setColor(UIManager.getColor("controlText"));
        g.setFont(FONT);
        g.drawString("nil",
          x+(w - g.getFontMetrics(FONT).stringWidth("nil"))/ 2,
          y+(h + g.getFontMetrics(FONT).getAscent())/2);
      }
    }

    public int getIconWidth() {
      return swatchWidth;
    }

    public int getIconHeight() {
      return swatchHeight;
    }
  }
}
{'fraction_non_alphanumeric': 0.10861618798955613, 'fraction_numerical': 0.010443864229765013, 'mean_word_length': 2.851206434316354, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4900407', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1787, 'n_words': 596}