/* * Copyright 2010-2014 the original author or authors. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package de.schildbach.pte.dto; import java.io.Serializable; /** * @author Andreas Schildbach */ public class Style implements Serializable { private static final long serialVersionUID = 7145603493425043304L; public final Shape shape; public final int backgroundColor; public final int foregroundColor; public final int borderColor; public enum Shape { RECT, ROUNDED, CIRCLE } public Style(final int backgroundColor, final int foregroundColor) { this.shape = Shape.ROUNDED; this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; this.borderColor = 0; } public Style(final Shape shape, final int backgroundColor, final int foregroundColor) { this.shape = shape; this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; this.borderColor = 0; } public Style(final Shape shape, final int backgroundColor, final int foregroundColor, final int borderColor) { this.shape = shape; this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; this.borderColor = borderColor; } public Style(final int backgroundColor, final int foregroundColor, final int borderColor) { this.shape = Shape.ROUNDED; this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; this.borderColor = borderColor; } public final boolean hasBorder() { return borderColor != 0; } public static final int BLACK = 0xFF000000; public static final int DKGRAY = 0xFF444444; public static final int GRAY = 0xFF888888; public static final int LTGRAY = 0xFFCCCCCC; public static final int WHITE = 0xFFFFFFFF; public static final int RED = 0xFFFF0000; public static final int GREEN = 0xFF00FF00; public static final int BLUE = 0xFF0000FF; public static final int YELLOW = 0xFFFFFF00; public static final int CYAN = 0xFF00FFFF; public static final int MAGENTA = 0xFFFF00FF; public static final int TRANSPARENT = 0; public static int parseColor(final String colorString) { if (colorString.charAt(0) == '#') { // Use a long to avoid rollovers on #ffXXXXXX long color = Long.parseLong(colorString.substring(1), 16); if (colorString.length() == 7) { // Set the alpha value color |= 0x00000000ff000000; } else if (colorString.length() != 9) { throw new IllegalArgumentException("Unknown color"); } return (int) color; } throw new IllegalArgumentException("Unknown color"); } public static int rgb(final int red, final int green, final int blue) { return (0xFF << 24) | (red << 16) | (green << 8) | blue; } public static int red(final int color) { return (color >> 16) & 0xff; } public static int green(final int color) { return (color >> 8) & 0xff; } public static int blue(final int color) { return color & 0xff; } public static int deriveForegroundColor(final int backgroundColor) { // formula for perceived brightness computation: http://www.w3.org/TR/AERT#color-contrast final double a = 1 - (0.299 * Style.red(backgroundColor) + 0.587 * Style.green(backgroundColor) + 0.114 * Style.blue(backgroundColor)) / 255; // dark colors, white font. Or light colors, black font if (a >= 0.5) return WHITE; else return BLACK; } }