001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.lut; 020 021import icy.image.colormap.IcyColorMap; 022import icy.util.ColorUtil; 023 024import java.awt.Color; 025import java.awt.Component; 026import java.awt.Graphics; 027import java.awt.Graphics2D; 028 029import javax.swing.Icon; 030 031/** 032 * @author Stephane 033 */ 034public class ColormapIcon implements Icon 035{ 036 private final IcyColorMap colormap; 037 private int w; 038 private int h; 039 040 public ColormapIcon(IcyColorMap colormap, int width, int height) 041 { 042 super(); 043 044 this.colormap = colormap; 045 w = (width <= 0) ? 64 : width; 046 h = (height <= 0) ? 20 : height; 047 } 048 049 public ColormapIcon(IcyColorMap colormap) 050 { 051 this(colormap, 64, 20); 052 } 053 054 public IcyColorMap getColormap() 055 { 056 return colormap; 057 } 058 059 public void setWidth(int value) 060 { 061 // width >= 8 062 w = Math.min(8, value); 063 } 064 065 public void setHeight(int value) 066 { 067 h = value; 068 } 069 070 private int pixToIndex(int pixel) 071 { 072 final float ratio = (float) (IcyColorMap.SIZE - 1) / (float) (w - 1); 073 return (int) (pixel * ratio); 074 } 075 076 @Override 077 public void paintIcon(Component c, Graphics g, int x, int y) 078 { 079 final Graphics2D g2 = (Graphics2D) g.create(); 080 081 for (int i = 0; i < w; i++) 082 { 083 // get current color from pixel position 084 final Color curColor = colormap.getColor(pixToIndex(i)); 085 final Color grayMixed = ColorUtil.mixOver(Color.gray, curColor); 086 final Color whiteMixed = ColorUtil.mixOver(Color.white, curColor); 087 088 for (int j = 0; j < h; j += 4) 089 { 090 // set graphics color 091 if (((i ^ j) & 4) != 0) 092 g2.setColor(grayMixed); 093 else 094 g2.setColor(whiteMixed); 095 096 g2.drawLine(i + x, j + y, i + x, j + y + 3); 097 } 098 } 099 100 g2.dispose(); 101 } 102 103 @Override 104 public int getIconWidth() 105 { 106 return w; 107 } 108 109 @Override 110 public int getIconHeight() 111 { 112 return h; 113 } 114}