TITLE:RGBImageFilterでアイコンの色調を変更

RGBImageFilterでアイコンの色調を変更

編集者:Terai Atsuhiro~

作成日:2006-08-21
更新日:2022-11-16 (水) 16:41:34
  • category: swing folder: RatingLabel title: RGBImageFilterでアイコンの色調を変更 tags: [ImageIcon, RGBImageFilter, JLabel] author: aterai pubdate: 2006-08-21T11:55:27+09:00 description: RGBImageFilterで色調を変更したアイコンの用意し、評価用コンポーネントを作成します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRfRNaARI/AAAAAAAAAhQ/8Rj6Rw8bkwU/s800/RatingLabel.png hreflang:
       href: https://java-swing-tips.blogspot.com/2008/12/jlabel-star-rating-bar.html
       lang: en

概要

RGBImageFilterで色調を変更したアイコンの用意し、評価用コンポーネントを作成します。

概要

RGBImageFilterで色調を変更したアイコンの用意し、評価用コンポーネントを作成します。

#screenshot

サンプルコード

#spanend
#spanadd
private final ImageProducer ip = orgIcon.getImage().getSource();
#spanend

#spandel
**サンプルコード [#a85a2963]
#spanend
 private final ImageProducer ip = orgIcon.getImage().getSource();
 private MyLabel makeLabel(float[] filter) {
   SelectedImageFilter sif = new SelectedImageFilter(filter);
   ImageIcon imageIcon = new ImageIcon(
     createImage(new FilteredImageSource(ip, sif)));
   return new MyLabel(imageIcon);
 }
 private class SelectedImageFilter extends RGBImageFilter {
   private final float[] filter;
   public SelectedImageFilter(float[] filter) {
     this.filter = filter;
     canFilterIndexColorModel = true;
   }
   public int filterRGB(int x, int y, int argb) {
     Color color = new Color(argb, true);
       float[] array = new float[4];
       color.getComponents(array);
       return new Color(array[0]*filter[0],
                        array[1]*filter[1],
                        array[2]*filter[2],
                        array[3]).getRGB();
     }
   }
 }
#spanadd
private static ImageIcon makeStarImageIcon(
#spanend
    ImageProducer ip, float rf, float gf, float bf) {
  return new ImageIcon(Toolkit.getDefaultToolkit().createImage(
    new FilteredImageSource(ip, new SelectedImageFilter(rf, gf, bf))));
#spanadd
}
#spanend

-&jnlp;
-&jar;
-&zip;
#spanadd
class SelectedImageFilter extends RGBImageFilter {
#spanend
  private final float rf;
  private final float gf;
  private final float bf;

#spandel
**解説 [#s11c2ccf]
#spanend
#spandel
RGBImageFilterを使用して、一つのアイコンから複数の色の異なるアイコンを生成しています。
#spanend
  protected SelectedImageFilter(float rf, float gf, float bf) {
    super();
    this.rf = Math.min(1f, rf);
    this.gf = Math.min(1f, gf);
    this.bf = Math.min(1f, bf);
    canFilterIndexColorModel = false;
  }

#spandel
上記のサンプルは、アマゾンなど((いろんな所でよく見かけるのですが、オリジナルは何処なんでしょうか?))でよく見かける5段階の評価を行うコンポーネントです。クリックしたアイコンの位置が評価レベルになります。
#spanend
  @Override public int filterRGB(int x, int y, int argb) {
    int r = (int) (((argb >> 16) & 0xFF) * rf);
    int g = (int) (((argb >> 8) & 0xFF) * gf);
    int b = (int) ((argb & 0xFF) * bf);
    return (argb & 0xFF_00_00_00) | (r << 16) | (g << 8) | (b);
  }
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin

参考リンク

解説

上記のサンプルはRGBImageFilterを使用して1つのアイコンから複数の色の異なるアイコンを生成し、5段階の評価を行うコンポーネントを作成しています。クリックしたアイコンの位置が評価レベルになります。

コメント

参考リンク

コメント