Convert an integer to an HTML RGB value. - Java java.lang

Java examples for java.lang:String HTML

Description

Convert an integer to an HTML RGB value.

Demo Code

/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   ww  w . jav a  2  s.  c o m
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        int rgb = 42;
        System.out.println(toRgbText(rgb));
    }

    /**
     * Convert an integer to an HTML RGB value. The result is of the form
     * #hhhhhh. The input rgb integer value will be clipped into the range 0 ~
     * 0xFFFFFF
     * 
     * @param rgb
     *            the integer RGB value
     * @return the value as an HTML RGB string
     */

    public static String toRgbText(int rgb) {
        // clip input value.

        if (rgb > 0xFFFFFF)
            rgb = 0xFFFFFF;
        if (rgb < 0)
            rgb = 0;

        String str = "000000" + Integer.toHexString(rgb); //$NON-NLS-1$ 
        return "#" + str.substring(str.length() - 6); //$NON-NLS-1$ 
    }
}

Related Tutorials