Java Color to Hex toHexString(Color c)

Here you can find the source of toHexString(Color c)

Description

Converts a color to its hexadecimal string representation for VzText e.g.

License

LGPL

Parameter

Parameter Description
Color a parameter

Declaration

public static String toHexString(Color c) 

Method Source Code


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

import java.awt.*;

public class Main {
    /**//w  w  w  .  ja  v  a  2 s.co  m
     * Converts a color to its hexadecimal string representation for VzText
     * e.g. #AARRGGBB
     * @param Color
     */
    public static String toHexString(Color c) {
        // Either one or two characters long
        String r = Integer.toHexString(c.getRed());
        String g = Integer.toHexString(c.getGreen());
        String b = Integer.toHexString(c.getBlue());
        String a = Integer.toHexString(c.getAlpha());

        if (r.length() == 1)
            r = "0" + r;
        if (g.length() == 1)
            g = "0" + g;
        if (b.length() == 1)
            b = "0" + b;
        if (a.length() == 1)
            a = "0" + a;

        return "#" + a + r + g + b;
    }

    /**
     * Converts a color to its hexadecimal string representation for VzText and
     * e.g. #AARRGGBB
     * @param int color in aarrggbb format (as from getRGB())
     */
    public static String toHexString(int color) {
        // Either one or two characters long
        String r = Integer.toHexString((color >> 16) & 0xFF);
        String g = Integer.toHexString((color >> 8) & 0xFF);
        String b = Integer.toHexString((color >> 0) & 0xFF);
        String a = Integer.toHexString((color >> 24) & 0xFF);

        if (r.length() == 1)
            r = "0" + r;
        if (g.length() == 1)
            g = "0" + g;
        if (b.length() == 1)
            b = "0" + b;
        if (a.length() == 1)
            a = "0" + a;

        return "#" + a + r + g + b;
    }
}

Related

  1. toHexCode(String prefix, Color color)
  2. toHexColor(Color col)
  3. toHexColor(Color col)
  4. toHexColor(final Color color)
  5. toHexColor(final Color color)
  6. toHexString(Color col)
  7. toHexString(Color col)
  8. toHexString(Color color)
  9. toHexString(Color color)