Java tutorial
//package com.java2s; /******************************************************************************* * Copyright 2012-present Pixate, 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 { /** * Convert a color to a HSL array. * * @param color The color to convert. * @param hsl A size-3 array to load with the HSL values. */ public static void colorToHsl(int color, float[] hsl) { float r = ((0x00ff0000 & color) >> 16) / 255.0F; float g = ((0x0000ff00 & color) >> 8) / 255.0F; float b = ((0x000000ff & color)) / 255.0F; float max = Math.max(Math.max(r, g), b); float min = Math.min(Math.min(r, g), b); float c = max - min; float hTemp = 0.0F; if (c == 0) { hTemp = 0; } else if (max == r) { hTemp = (float) (g - b) / c; if (hTemp < 0) hTemp += 6.0F; } else if (max == g) { hTemp = (float) (b - r) / c + 2.0F; } else if (max == b) { hTemp = (float) (r - g) / c + 4.0F; } float h = 60.0F * hTemp; float l = (max + min) * 0.5F; float s; if (c == 0) { s = 0.0F; } else { s = c / (1 - Math.abs(2.0F * l - 1.0F)); } hsl[0] = h; hsl[1] = s; hsl[2] = l; } }