Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Color;

public class Main {
    public static int ensureLight(int color) {

        float hsl[] = new float[3];
        rgbToHsl(color, hsl);

        if (hsl[2] < 0.54f) {
            hsl[2] = 0.54f;
            color = hslToRgb(hsl);
        }
        return color;
    }

    private static void rgbToHsl(int color, float hsl[]) {

        float r = Color.red(color) / 255.f;
        float g = Color.green(color) / 255.f;
        float b = Color.blue(color) / 255.f;
        float max = Math.max(Math.max(r, g), b);
        float min = Math.min(Math.min(r, g), b);
        float h;
        float s;
        float l;

        h = s = l = (max + min) / 2;

        if (max == min) {
            h = s = 0;
        } else {
            float delta = max - min;
            s = l > 0.5f ? delta / (2f - max - min) : delta / (max + min);
            if (max == r) {
                h = (g - b) / delta;
            } else if (max == g) {
                h = (b - r) / delta + 2f;
            } else if (max == b) {
                h = (r - g) / delta + 4f;
            }
            h *= 60f;
            if (h < 0f) {
                h += 360f;
            } else if (h > 360f) {
                h -= 360f;
            }
        }
        hsl[0] = h;
        hsl[1] = s;
        hsl[2] = l;
    }

    private static int hslToRgb(float hsl[]) {
        float h = hsl[0] / 360.f;
        float s = hsl[1];
        float l = hsl[2];

        int r;
        int g;
        int b;

        if (s == 0) {
            r = g = b = (int) (l * 255f);
        } else {
            float t1 = l < 0.5 ? l * (1 + s) : l + s - l * s;
            float t2 = 2 * l - t1;
            r = (int) (hueToMagnitude(t1, t2, h + 1 / 3f) * 255f);
            g = (int) (hueToMagnitude(t1, t2, h) * 255f);
            b = (int) (hueToMagnitude(t1, t2, h - 1 / 3f) * 255f);
        }
        return Color.rgb(r, g, b);
    }

    private static float hueToMagnitude(float t1, float t2, float h) {
        h += h > 1 ? -1 : h < 0 ? 1 : 0;
        float result = 0;
        if (h * 6 < 1) {
            result = t2 + (t1 - t2) * 6f * h;
        } else if (h * 2 < 1) {
            result = t1;
        } else if (h * 3 < 2) {
            result = t2 + (t1 - t2) * (2f / 3f - h) * 6f;
        } else {
            result = t2;
        }
        return result;
    }
}