Java RGB Color Convert To rgbToHex(String color)

Here you can find the source of rgbToHex(String color)

Description

rgb To Hex

License

Open Source License

Declaration

private static String rgbToHex(String color) 

Method Source Code

//package com.java2s;
/* /*from   w ww . ja  v  a 2 s.  c o m*/
 * Copyright (C) 2017 Marc Magon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a colour to hex i suppose
     */
    private static final String chars = "0123456789ABCDEF";

    private static String rgbToHex(String color) {
        //System.out.println("RGB TO HEX [" + color + "]");
        String[] RGB = color.split(",");
        return toHex(RGB[0]) + toHex(RGB[1]) + toHex(RGB[2]);
    }

    private static String toHex(String color) {
        //System.out.println("TO HEX [" + color + "]");
        //return Integer.toHexString(Integer.parseInt(color));

        //cant do as it truncates the 09 to 9
        //       System.out.println(Integer.toHexString(Integer.parseInt(color)));
        if (color == null) {
            return "00";
        }
        Integer N;
        N = Integer.parseInt(color);
        if (N == 0) {
            return "00";
        }
        N = Math.max(0, N);
        N = Math.min(N, 255);
        N = Math.round(N);
        char s = chars.charAt((N - N % 16) / 16);
        char s1 = chars.charAt(N % 16);
        return "" + s + s1;// "0123456789ABCDEF".charAt((N-N%16)/16)+ "0123456789ABCDEF".charAt(N%16);
    }
}

Related

  1. rgbToHex(final int r, final int g, final int b)
  2. rgbToHex(int r, int g, int b)
  3. RGBtoHex(int r, int g, int b)
  4. rgbToHex(int[] rgb)
  5. RGBToHex(int[] rgb)
  6. RGBtoHSB(int r, int g, int b, float hsbvals[])
  7. RGBtoHSB(int r, int g, int b, float[] hsbvals)
  8. RGBtoHSB(int red, int green, int blue, float hsb[])
  9. rgbToHsl(byte red, byte green, byte blue)