001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.base;
018
019import com.google.common.collect.Maps;
020
021import javax.annotation.concurrent.Immutable;
022import java.util.Map;
023
024/**
025 * Represents an RGB color. This class is immutable, any changes will
026 * return a new instance and make no modifications to the original
027 */
028@Immutable
029public class Color {
030    public static final Color WHITE = fromRGB(0xFFFFFF);
031    public static final Color SILVER = fromRGB(0xC0C0C0);
032    public static final Color GRAY = fromRGB(0x808080);
033    public static final Color BLACK = fromRGB(0x000000);
034    public static final Color RED = fromRGB(0xFF0000);
035    public static final Color MAROON = fromRGB(0x800000);
036    public static final Color YELLOW = fromRGB(0xFFFF00);
037    public static final Color OLIVE = fromRGB(0x808000);
038    public static final Color LIME = fromRGB(0x00FF00);
039    public static final Color GREEN = fromRGB(0x008000);
040    public static final Color TEAL = fromRGB(0x008080);
041    public static final Color BLUE = fromRGB(0x0000FF);
042    public static final Color NAVY = fromRGB(0x000080);
043    public static final Color FUCHSIA = fromRGB(0xFF00FF);
044    public static final Color PURPLE = fromRGB(0x800080);
045    public static final Color ORANGE = fromRGB(0xFFA500);
046
047    private static Map<Integer, Color> cache = Maps.newConcurrentMap();
048
049    private final byte red;
050    private final byte green;
051    private final byte blue;
052
053    private Color(int red, int green, int blue) {
054        this.red = (byte) red;
055        this.green = (byte) green;
056        this.blue = (byte) blue;
057    }
058
059    public static Color fromRGB(int red, int green, int blue) {
060        int id = red << 16 | green << 8 | blue;
061
062        if (!cache.containsKey(id)) {
063            cache.put(id, new Color(red, green, blue));
064        }
065
066        return cache.get(id);
067    }
068
069    public static Color fromRGB(int rgb) {
070        return fromRGB(rgb >> 16 & 0xff, rgb >> 8 & 0xff, rgb & 0xff);
071    }
072
073    public int red() {
074        return 0xff & red;
075    }
076
077    public int green() {
078        return 0xff & green;
079    }
080
081    public int blue() {
082        return 0xff & blue;
083    }
084
085    public Color setRed(int red) {
086        return fromRGB(red, green, blue);
087    }
088
089    public Color setGreen(int green) {
090        return fromRGB(red, green, blue);
091    }
092
093    public Color setBlue(int blue) {
094        return fromRGB(red, green, blue);
095    }
096
097    public int asRGB() {
098        return red() << 16 | green() << 8 | blue;
099    }
100
101    public int asBGR() {
102        return blue() << 16 | green() << 8 | red;
103    }
104}