/*
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.typescript.text;

import java.util.Map;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;

/**
 * Converts RGB values into Colors.
 *
 * @author tyleradams
 */
public final class ColorManager {

    private final Map<RGB, Color> colorTable = Maps.newHashMap();

    public Color getColor(RGB rgb) {
        Preconditions.checkNotNull(rgb);

        Color color = this.colorTable.get(rgb);
        if (color == null) {
            color = new Color(Display.getCurrent(), rgb);
            this.colorTable.put(rgb, color);
        }

        return color;
    }

    public void dispose() {
        for (Color color : this.colorTable.values()) {
            color.dispose();
        }
    }
}

/*
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.typescript.text;

import static com.google.common.base.Preconditions.checkNotNull;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
 * The color manager creates and disposes of colors.
 *
 * @author tyleradams
 */
public final class ColorManager {

    private final LoadingCache<RGB, Color> colors = CacheBuilder.newBuilder().build(new CacheLoader<RGB, Color>() {
        @Override
        public Color load(RGB rgb) throws Exception {
            return new Color(Display.getCurrent(), rgb);
        }
    });

    public Color getColor(RGB rgb) {
        checkNotNull(rgb);

        return this.colors.getUnchecked(rgb);
    }

    public void dispose() {
        for (Color color : this.colors.asMap().values()) {
            color.dispose();
        }
    }
}

Use a cache for the colors