Java tutorial
//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. */ import android.graphics.Color; public class Main { /** * Calculates the luminance value of a given {@code int} representation of {@link Color}. * <p> * Derived from formula at http://gmazzocato.altervista.org/colorwheel/algo.php * * @param color The {@link Color} to evaluate * @return the luminance value of the given color */ public static double calculateLuminance(int color) { final double[] sRGB = new double[3]; sRGB[0] = Color.red(color) / 255.0d; sRGB[1] = Color.green(color) / 255.0d; sRGB[2] = Color.blue(color) / 255.0d; final double[] lumRGB = new double[3]; for (int i = 0; i < sRGB.length; ++i) { lumRGB[i] = (sRGB[i] <= 0.03928d) ? sRGB[i] / 12.92d : Math.pow(((sRGB[i] + 0.055d) / 1.055d), 2.4d); } return 0.2126d * lumRGB[0] + 0.7152d * lumRGB[1] + 0.0722d * lumRGB[2]; } }