/*
 * Licensed to the Technische Universität Darmstadt under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The Technische Universität Darmstadt
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.
 *
 * 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 de.tudarmstadt.ukp.clarin.webanno.support;

public class Coloring
{
    // http://24ways.org/2010/calculating-color-contrast/
    // http://stackoverflow.com/questions/11867545/change-text-color-based-on-brightness-of-the-covered-background-area
    public static String bgToFgColor(String hexcolor)
    {
        try {
            int r = Integer.parseInt(hexcolor.substring(1, 3), 16);
            int g = Integer.parseInt(hexcolor.substring(3, 5), 16);
            int b = Integer.parseInt(hexcolor.substring(5, 7), 16);
            double yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
            return (yiq >= 128) ? "#000000" : "#ffffff";
        }
        catch (Exception e) {
            return "#000000";
        }
    }
}
