Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 * 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;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import java.text.ParseException;

public class Main {
    private static final String TAG = "ColorUtils";
    public static final float DEFAULT_BLOCK_SATURATION = .84f;
    public static final float DEFAULT_BLOCK_VALUE = 0.72f;

    /**
     * Parses a string as an opaque color, either as a decimal hue (example: {@code 330}) using a
     * standard set of saturation and value) or as six digit hex code (example: {@code #BB66FF}).
     * If the string is null or cannot  otherwise be parsed, any error is logged and
     * {@code defaultColor} is returned.
     *
     * @param str The input to parse.
     * @param tempHsvArray An optional previously allocated array for HSV calculations.
     * @param defaultColor The default color to return if the color cannot be parsed.
     * @return The parsed color, in {@code int} form.
     */
    public static int parseColor(@Nullable String str, @Nullable float[] tempHsvArray, int defaultColor) {
        if (str == null) {
            return defaultColor;
        }
        str = str.trim();
        if (str.isEmpty()) {
            return defaultColor;
        }
        try {
            return parseColor(str, tempHsvArray);
        } catch (ParseException e) {
            Log.w(TAG, e.toString());
            return defaultColor;
        }
    }

    /**
     * Parses a string as an opaque color, either as a decimal hue (example: {@code 330}) using a
     * standard set of saturation and value) or as six digit hex code (example: {@code #BB66FF}).
     * If the string cannot be parsed, a {@link ParseException} is thrown.
     *
     * @param str The input to parse.
     * @param tempHsvArray An optional previously allocated array for HSV calculations.
     * @return The parsed color, in {@code int} form.
     * @throws ParseException
     */
    public static int parseColor(@NonNull String str, @Nullable float[] tempHsvArray) throws ParseException {
        Integer result = null;

        char firstChar = str.charAt(0);
        if (firstChar == '#' && str.length() == 7) {
            try {
                result = Integer.parseInt(str.substring(1, 7), 16);
            } catch (NumberFormatException e) {
                throw new ParseException("Invalid hex color: " + str, 0);
            }
            return result;
        } else if (Character.isDigit(firstChar) && str.length() <= 3) {
            try {
                int hue = Integer.parseInt(str);
                result = getBlockColorForHue(hue, tempHsvArray);
            } catch (NumberFormatException e) {
                throw new ParseException("Invalid color hue: " + str, 0);
            }
        }
        // Maybe other color formats? 3 digit hex, CSS color functions, etc.
        return result;
    }

    /**
     * Converts a hue number to a standard ARGB {@code int}, using {@link #DEFAULT_BLOCK_SATURATION}
     * and {@link #DEFAULT_BLOCK_VALUE}.  The resulting color will always be opaque.
     *
     * @param hue The hue to convert.
     * @param tempHsvArray An optional previously allocated array for HSV calculations.
     * @return The color as an ARGB {@code int}.
     */
    public static int getBlockColorForHue(int hue, @Nullable float[] tempHsvArray) {
        hue = ((hue % 360) + 360) % 360; // Clamp to 0-359

        if (tempHsvArray == null) {
            tempHsvArray = new float[3];
        }
        tempHsvArray[0] = hue;
        tempHsvArray[1] = DEFAULT_BLOCK_SATURATION;
        tempHsvArray[2] = DEFAULT_BLOCK_VALUE;
        return Color.HSVToColor(tempHsvArray);
    }
}