/*** * * Copyright (C) 2008 Alessandro La Rosa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Contact: alessandro.larosa@gmail.com * * Author: Alessandro La Rosa */ import javax.microedition.lcdui.Graphics; public class ColorFadeText { public int[] colors = null; public int fadeDuration = 0; public long startTime = 0; boolean started = false; public String text = null; public ColorFadeText(String text, int[] colors, int fadeDuration) { if(colors.length == 0) { throw new IllegalArgumentException("You must define at least 1 color"); } this.text = text; this.colors = colors; this.fadeDuration = fadeDuration; } public void start() { startTime = System.currentTimeMillis(); started = true; } public void paint(Graphics g, int x, int y, int anchor) { if(started) { long diff = System.currentTimeMillis() - startTime; int module = (int)(diff % fadeDuration); int colorIndex = (int)(diff / fadeDuration) % colors.length; int midColor = midColor( colors[(colorIndex + 1) % colors.length], colors[colorIndex], module, fadeDuration ); g.setColor(midColor); } else { g.setColor(colors[0]); } g.drawString(text, x, y, anchor); } static int midColor(int color1, int color2, int prop, int max) { int red = (((color1 >> 16) & 0xff) * prop + ((color2 >> 16) & 0xff) * (max - prop)) / max; int green = (((color1 >> 8) & 0xff) * prop + ((color2 >> 8) & 0xff) * (max - prop)) / max; int blue = (((color1 >> 0) & 0xff) * prop + ((color2 >> 0) & 0xff) * (max - prop)) / max; int color = red << 16 | green << 8 | blue; return color; } }