Skip to main content
3 of 4
deleted 4 characters in body
Kevin Cruijssen
  • 131.5k
  • 13
  • 144
  • 384

#Java 10, 228 227 224 182 bytes

s->{var x="$1$1";int l=s.length();return l>6?s:l>5?s+s.charAt(5):l<2?"#000000":s.replaceAll(l>4?"(.)(.)(.)(.)$":l==3?"([^#]{2})":"([^#])",l>4?"$1$4$2$4$3$4":l>3?x:l>2?x+"$1":x+x+x);}

Try it online.

Explanation:

s->{                      // Method with String as both parameter and return-type
  var x="$1$1";         //  Create a temp String to repeat a match
  int l=s.length();     //  Length of the String
  return l>6?           //  If the length is 7:
    s                   //   Return the input-String as is
   :l>5?                //  Else-if the length is 6:
    s+s.charAt(5)       //   Return the input-String with the last character repeated
   :l<2?                //  Else-if the length is 1:
    "#000000";          //   Simply return the literal String #000000
   :                    //  Else (the length is 2, 3, 4, or 5):
    s.replaceAll(       //   Return the input-String after a regex replace:
                        //    With as match:
     l>4?               //     If the length is 5:
      "(.)(.)(.)(.)$",  //      Use a match for pattern #(A)(B)(C)(D)
     :l==3?             //     Else-if the length is 3:
      "([^#]{2})"       //      Use a match for pattern #(AB)
     :                  //     Else (the length is 2 or 4):
      "([^#])",         //      Use a match for pattern #(A) or #(A)(B)(C)
                        //    And as replacement: 
     l>4?               //     If the length is 5:
      "$1$4$2$4$3$4"    //      Change #ABCD to #ADBDCD
     :l>3?              //     Else-if the length is 4:
      x                 //      Change #ABC to #AABBCC
     :l>2?              //     Else-if the length is 3:
      x+"$1"            //      Change #AB to #ABABAB
     :                  //     Else (the length is 2):
      x+x+x);}          //      Change #A to #AAAAAA
Kevin Cruijssen
  • 131.5k
  • 13
  • 144
  • 384