Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RGBToHexConverter {
- // Trying to make this as shitty as possible.
- public static String[] hexColors = new String[256 * 256 * 256];
- public RGBToHexConverter() {
- generateTable();
- }
- public static void generateTable() {
- for(int i = 0; i < hexColors.length; i++) {
- hexColors[i] = String.format("%06x", i);
- }
- }
- private int getColorInt(int r, int g, int b) {
- int color = r << 16 | g << 8 | b;
- return color;
- }
- private String rgb(int r, int g, int b) {
- // Checking to make sure the colors stay within the bounds
- r = r > -1 && r < 256 ? r : r & 0xff;
- g = g > -1 && g < 256 ? g : g & 0xff;
- b = b > -1 && b < 256 ? b : b & 0xff;
- int colorIndex = getColorInt(r, g, b);
- String colorCode = hexColors[colorIndex];
- return "#" + colorCode;
- }
- public static void main(String[] args) {
- RGBToHexConverter converter = new RGBToHexConverter();
- System.out.println(converter.rgb(47, 91, 105));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment