Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Color;

public class Main {
    /**
     * Converts a hexadecimal color to Color object
     * e.g. ff0000 will be converted to Color 255 0 0
     * @param hex the hexadecimal color
     * @return Color object of the hexadecimal value
     */
    static Color hexToColor(final String hex) {

        final int hexColorDigitSize = 6;
        final int hexRadix = 16;
        final int hexRgbDigitSize = 2;
        final int redStartIndex = 0;
        final int greenStartIndex = 2;
        final int blueStartIndex = 4;

        if (hex == null || hex.length() != hexColorDigitSize) {
            throw new IllegalArgumentException("Invalid hex color value.");
        }

        int red = Integer.parseInt(hex.substring(redStartIndex, redStartIndex + hexRgbDigitSize), hexRadix);
        int green = Integer.parseInt(hex.substring(greenStartIndex, greenStartIndex + hexRgbDigitSize), hexRadix);
        int blue = Integer.parseInt(hex.substring(blueStartIndex, blueStartIndex + hexRgbDigitSize), hexRadix);
        return new Color(red, green, blue);
    }
}