Java Color Create toColor(final String hexString)

Here you can find the source of toColor(final String hexString)

Description

In a lot of HTML documents and still others, colors are represented using the RGB values, concatenated as hexadecimal strings.

License

Open Source License

Parameter

Parameter Description
hexString The hexadecimal string (with # prefix) to be translated to a color

Return

The color represented by this hexadecimal string

Declaration


public static Color toColor(final String hexString) 

Method Source Code


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

public class Main {
    /**/*from w  w  w  .ja v  a 2  s  . c  om*/
     * In a lot of HTML documents and still others, colors are represented using the RGB values, concatenated as
     * hexadecimal strings. This function is used to create a color object from such a hexadecimal string.
     * 
     * @param hexString The hexadecimal string (with # prefix) to be translated to a color
     * @return The color represented by this hexadecimal string
     */

    public static Color toColor(final String hexString) {
        return new Color(Character.digit(hexString.charAt(1), 16) * 16 + Character.digit(hexString.charAt(2), 16),
                Character.digit(hexString.charAt(3), 16) * 16 + Character.digit(hexString.charAt(4), 16),
                Character.digit(hexString.charAt(5), 16) * 16 + Character.digit(hexString.charAt(6), 16));
    }
}

Related

  1. generateTexturePaint(String text, Font font, Color textColor, Color bgColor, int width, int height)
  2. generateVisuallyDistinctColors(int ncolors, float minComponent, float maxComponent)
  3. toColor(byte red, byte green, byte blue)
  4. toColor(byte[] bytes)
  5. toColor(final String hexColor)
  6. toColor(final String text, final Color defaultValue)
  7. toColor(float[] color)
  8. toColor(int rgba)
  9. toColor(int x)