Simple colors parsing method.

Discussion in 'Resources' started by NuclearW, Aug 17, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    NuclearW

    I haven't really seen this anywhere, but this is a very basic method I've made to convert the & color codes to the respective ChatColor constants for messages.

    PHP:
    public String colorizeText(String string) {
        
    string string.replaceAll("&0"ChatColor.BLACK+"");
        
    string string.replaceAll("&1"ChatColor.DARK_BLUE+"");
        
    string string.replaceAll("&2"ChatColor.DARK_GREEN+"");
        
    string string.replaceAll("&3"ChatColor.DARK_AQUA+"");
        
    string string.replaceAll("&4"ChatColor.DARK_RED+"");
        
    string string.replaceAll("&5"ChatColor.DARK_PURPLE+"");
        
    string string.replaceAll("&6"ChatColor.GOLD+"");
        
    string string.replaceAll("&7"ChatColor.GRAY+"");
        
    string string.replaceAll("&8"ChatColor.DARK_GRAY+"");
        
    string string.replaceAll("&9"ChatColor.BLUE+"");
        
    string string.replaceAll("&a"ChatColor.GREEN+"");
        
    string string.replaceAll("&b"ChatColor.AQUA+"");
        
    string string.replaceAll("&c"ChatColor.RED+"");
        
    string string.replaceAll("&d"ChatColor.LIGHT_PURPLE+"");
        
    string string.replaceAll("&e"ChatColor.YELLOW+"");
        
    string string.replaceAll("&f"ChatColor.WHITE+"");
        return 
    string;
    }
     
  2. Offline

    Coryf88

    PHP:
    public String colorizeText(String string) {
        for (
    ChatColor color ChatColor.values()) {
            
    string string.replace(String.format("&%x"color.getCode()), color.toString());
        }
        return 
    string;
    }
     
    Cirno, r3Fuze and NuclearW like this.
  3. Offline

    krinsdeath

    Code:
    public class Utility {
      private final static Pattern COLOR = Pattern.compile("(?i)&([0-F])");
      public static String color(String line) {
        return COLOR.matcher(line).replaceAll("\u00A7$1");
      }
    }
    
     
  4. Offline

    ferrybig

    ChatColor.translateAlternateColorCodes('&', textToTranslate)
     
  5. Offline

    NuclearW

    Quite the necropost, but yes, that's the new way of doing it.
     
  6. Offline

    p000ison

    I like this most:

    Code:
        /**
        * Convert color hex values
        *
        * @param msg
        * @return
        */
        public static String parseColors(String msg)
        {
            return msg.replace("&", "\u00a7");
        }
     
  7. Offline

    MeneXia

    I personally use this:

    Code:
    public String colorize(String message) {
            return message.replaceAll("&([a-f0-9])", ChatColor.COLOR_CHAR + "$1");
        }
     
  8. Offline

    xGhOsTkiLLeRx

    Code:
    string.replaceAll("&((?i)[0-9a-fk-or])", "\u00A7$1");
    Case INsensitive!
     
  9. Offline

    iKeirNez

    Thought I would post this, we use this in CommandsEX
    Code:
    public static String replaceChatColors(String s) {
            for (ChatColor c : ChatColor.values()) {
                s = s.replaceAll("&" + c.getChar(), ChatColor.getByChar(c.getChar()) + "");
            }
            return s;
        }
    Never knew there were so many ways to do something simple.
     
  10. Offline

    Double0negative

    Code:
           str =str.replaceAll("(&([a-fk-or0-9]))", "\u00A7$2");
    
     
  11. Offline

    chaseoes Retired Staff

    I use \u00A7$1 and it's always worked the same.. any difference?
     
  12. Offline

    Double0negative

    No idea tbh, this is just what i use and it works xD. Not very good at regex.
     
  13. Offline

    sayaad

    Code:java
    1. public static String parseColors(String msg){ return msg.replaceAll("&", "§"); }

    I win.
     
  14. Offline

    thescreem

    That would replace:

    "I like this & that!" to "I like this § that!" :p
     
  15. Offline

    Hidendra

    Am I the only one who stopped using Regex? because in some scenarios if you use it a lot it's stupid slow (in my case it was). it also replaces bold/other escape sequences now supported (not just colors despite the name)

    Code:
        /**
         * The escape sequence for minecraft special chat codes
         */
        public static final char ESCAPE = '\u00A7';
    
        /**
         * Replace all of the color codes (prepended with &) with the corresponding color code.
         * This uses raw char arrays so it can be considered to be extremely fast.
         *
         * @param text
         * @return
         */
        public static String replaceColors(String text) {
            char[] chrarray = text.toCharArray();
    
            for (int index = 0; index < chrarray.length; index ++) {
                char chr = chrarray[index];
                
                // Ignore anything that we don't want
                if (chr != '&') {
                    continue;
                }
    
                if ((index + 1) == chrarray.length) {
                    // we are at the end of the array
                    break;
                }
    
                // get the forward char
                char forward = chrarray[index + 1];
                
                // is it in range?
                if ((forward >= '0' && forward <= '9') || (forward >= 'a' && forward <= 'f') || (forward >= 'k' && forward <= 'r')) {
                    // It is! Replace the char we are at now with the escape sequence
                    chrarray[index] = ESCAPE;
                }
            }
    
            // Rebuild the string and return it
            return new String(chrarray);
        }
    
     
  16. Offline

    bitfreeze

    Nice and simple approach, Hidendra.
    Code:
                    // It is! Replace the char we are at now with the escape sequence
                    chrarray[index] = ESCAPE;
                } else {
                    if (forward == '&') {
                        index++;
                    }
                }
    
    This small addition would support && escapes.

    The .translateAlternateColorCodes method seems to be the most compatible solution, if permissions per color/formatting are not required. I'm going to take a look at it.
     
Thread Status:
Not open for further replies.

Share This Page