Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2014 Google 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.
 */

public class Main {
    /**
     * Calculates the contrast ratio of two order-independent luminance values.
     * <p>
     * Derived from formula at http://gmazzocato.altervista.org/colorwheel/algo.php
     *
     * @param lum1 The first luminance value
     * @param lum2 The second luminance value
     * @return The contrast ratio of the luminance values
     * @throws IllegalArgumentException if luminance values are < 0
     */
    public static double calculateContrastRatio(double lum1, double lum2) {
        if ((lum1 < 0.0d) || (lum2 < 0.0d)) {
            throw new IllegalArgumentException("Luminance values may not be negative.");
        }

        return (Math.max(lum1, lum2) + 0.05d) / (Math.min(lum1, lum2) + 0.05d);
    }
}