Advertisement
T99

TextStylizer.java

T99
Jun 16th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. /*
  2.  * Tested working on:
  3.  *  - IntelliJ on Linux
  4.  *  - Linux
  5.  *
  6.  * Does NOT work on:
  7.  *  - Eclipse
  8.  *  - Windows CLI
  9.  */
  10.  
  11. public class TextStylizer {
  12.    
  13.     public static String getStylizedText(String string, Color color) {
  14.        
  15.         return Attribute.NORMAL.getAttributeCode() + color.getColorCode() + string + getResetCode();
  16.        
  17.     }
  18.    
  19.     public static String getStylizedText(String string, Attribute attribute, Color color) {
  20.        
  21.         return attribute.getAttributeCode() + color.getColorCode() + string + getResetCode();
  22.        
  23.     }
  24.    
  25.     private static String getResetCode() {
  26.        
  27.         return "\033[0m";
  28.        
  29.     }
  30.    
  31.     public enum Attribute {
  32.  
  33.         NORMAL      ("\033[0;"),
  34.         BOLD        ("\033[1;"),
  35.         UNDERLINE   ("\033[4;");
  36.  
  37.         private final String attributeCode;
  38.  
  39.         Attribute(String attributeCode) {
  40.  
  41.             this.attributeCode = attributeCode;
  42.  
  43.         }
  44.  
  45.         String getAttributeCode() {
  46.  
  47.             return attributeCode;
  48.  
  49.         }
  50.  
  51.     }
  52.    
  53.     public enum Color {
  54.        
  55.         BLACK       ("30m"),
  56.         RED         ("31m"),
  57.         GREEN       ("32m"),
  58.         YELLOW      ("33m"),
  59.         BLUE        ("34m"),
  60.         PURPLE      ("35m"),
  61.         CYAN        ("36m"),
  62.         WHITE       ("37m"),
  63.         HIBLACK     ("90m"),
  64.         HIRED       ("91m"),
  65.         HIGREEN     ("92m"),
  66.         HIYELLOW    ("93m"),
  67.         HIBLUE      ("94m"),
  68.         HIPURPLE    ("95m"),
  69.         HICYAN      ("96m"),
  70.         HIWHITE     ("97m");
  71.        
  72.         private final String colorCode;
  73.        
  74.         Color(String colorCode) {
  75.            
  76.             this.colorCode = colorCode;
  77.            
  78.         }
  79.        
  80.         public String getColorCode() {
  81.            
  82.             return colorCode;
  83.            
  84.         }
  85.        
  86.         public Color getIntensityShiftedVersion() {
  87.            
  88.             return getIntensityShiftedVersion(this);
  89.            
  90.         }
  91.        
  92.         public static Color getIntensityShiftedVersion(Color color) {
  93.            
  94.             switch (color) {
  95.                
  96.                 case BLACK:
  97.                     return HIBLACK;
  98.                
  99.                 case RED:
  100.                     return HIRED;
  101.                
  102.                 case GREEN:
  103.                     return HIGREEN;
  104.                
  105.                 case YELLOW:
  106.                     return HIYELLOW;
  107.                
  108.                 case BLUE:
  109.                     return HIBLUE;
  110.                
  111.                 case PURPLE:
  112.                     return HIPURPLE;
  113.                
  114.                 case CYAN:
  115.                     return HICYAN;
  116.                
  117.                 case WHITE:
  118.                     return HIWHITE;
  119.                
  120.                 case HIBLACK:
  121.                     return BLACK;
  122.                
  123.                 case HIRED:
  124.                     return RED;
  125.                
  126.                 case HIGREEN:
  127.                     return GREEN;
  128.                
  129.                 case HIYELLOW:
  130.                     return YELLOW;
  131.                
  132.                 case HIBLUE:
  133.                     return BLUE;
  134.                
  135.                 case HIPURPLE:
  136.                     return PURPLE;
  137.                
  138.                 case HICYAN:
  139.                     return CYAN;
  140.                
  141.                 case HIWHITE:
  142.                     return WHITE;
  143.                    
  144.                 default:
  145.                     throw new IllegalArgumentException("That color doesn't exist -- I don't know how you did this.");
  146.                    
  147.             }
  148.            
  149.         }
  150.        
  151.     }
  152.    
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement