Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Functions to convert from a string to a colour constant and vice versa

    Functions
    1
    1
    244
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pianofire
      pianofire Fuze Team last edited by pianofire

      "stringToColour" and "colourToString" by @pianofire
      Share Code : NNQCWRNDNN

      // Functions to convert from a string to a colour constant and vice versa
      // Thanks to Nisse5 for the lookup table!
      struct StructColourDef
          string name = ""  
          vector clr = black
      endstruct
      
      StructColourDef colourDefs = [
         [.name = "black", .clr = black],
         [.name = "fuzeGrey", .clr = fuzeGrey],
         [.name = "darkGrey", .clr = darkGrey], 
         [.name = "grey", .clr = grey],
         [.name = "silver", .clr = silver],
         [.name = "lightGrey", .clr = lightGrey],
         [.name = "white", .clr = white],
         [.name = "maroon", .clr = maroon],
         [.name = "darkRed", .clr = darkRed],
         [.name = "red", .clr = red],
         [.name = "crimson", .clr = crimson],
         [.name = "fuzePink", .clr = fuzePink],
         [.name = "deepPink", .clr = deepPink],
         [.name = "hotPink", .clr = hotPink],
         [.name = "pink", .clr = pink],
         [.name = "lightPink", .clr = lightPink],
         [.name = "indigo", .clr = indigo],
         [.name = "purple", .clr = purple],
         [.name = "fuschia", .clr = fuschia],
         [.name = "orchid", .clr = orchid],
         [.name = "violet", .clr = violet],
         [.name = "plum", .clr = plum],
         [.name = "thistle", .clr = thistle],
         [.name = "lavender", .clr = lavender],
         [.name = "brown", .clr = brown],
         [.name = "sienna", .clr = sienna],
         [.name = "coral", .clr = coral],
         [.name = "darkSalmon", .clr = darkSalmon],
         [.name = "salmon", .clr = salmon],
         [.name = "lightSalmon", .clr = lightSalmon],
         [.name = "sand", .clr = sand],
         [.name = "tan", .clr = tan],
         [.name = "wheat", .clr = wheat],
         [.name = "bisque", .clr = bisque],
         [.name = "corn", .clr = corn],
         [.name = "deepOrange", .clr = deepOrange],
         [.name = "darkOrange", .clr = darkOrange],
         [.name = "orange", .clr = orange],
         [.name = "darkGold", .clr = darkGold],
         [.name = "gold", .clr = gold],
         [.name = "yellow", .clr = yellow],
         [.name = "khaki", .clr = khaki],
         [.name = "lightYellow", .clr = lightYellow],
         [.name = "darkGreen", .clr = darkGreen],
         [.name = "darkOlive", .clr = darkOlive],
         [.name = "olive", .clr = olive],
         [.name = "seaGreen", .clr = seaGreen],
         [.name = "green", .clr = green],
         [.name = "lime", .clr = lime],
         [.name = "teal", .clr = teal],
         [.name = "cadetBlue", .clr = cadetBlue],
         [.name = "turquoise", .clr = turquoise],
         [.name = "aquamarine", .clr = aquamarine],
         [.name = "cyan", .clr = cyan],
         [.name = "lightCyan", .clr = lightCyan],
         [.name = "midnight", .clr = midnight],
         [.name = "navy", .clr = navy],
         [.name = "blue", .clr = blue],
         [.name = "royalBlue", .clr = royalBlue],
         [.name = "cornFlower", .clr = cornFlower],
         [.name = "fuzeBlue", .clr = fuzeBlue],
         [.name = "skyBlue", .clr = skyBlue],
         [.name = "lightBlue", .clr = lightBlue],
         [.name = "charcoal", .clr = charcoal],
         [.name = "slate", .clr = slate],
         [.name = "ruby", .clr = ruby],
         [.name = "cerise", .clr = cerise],
         [.name = "magenta", .clr = magenta],
         [.name = "watermelon", .clr = watermelon],
         [.name = "roseGold", .clr = roseGold],
         [.name = "grape", .clr = grape],
         [.name = "raspberry", .clr = raspberry],
         [.name = "amethyst", .clr = amethyst],
         [.name = "lilac", .clr = lilac],
         [.name = "periwinkle", .clr = periwinkle],
         [.name = "rust", .clr = rust],
         [.name = "coffee", .clr = coffee],
         [.name = "bronze", .clr = bronze],
         [.name = "copper", .clr = copper],
         [.name = "cinnamon", .clr = cinnamon],
         [.name = "apricot", .clr = apricot],
         [.name = "peach", .clr = peach],
         [.name = "taupe", .clr = taupe],
         [.name = "beige", .clr = beige],
         [.name = "pumpkin", .clr = pumpkin],
         [.name = "caramel", .clr = caramel],
         [.name = "ginger", .clr = ginger],
         [.name = "yellowOchre", .clr = yellowOchre],
         [.name = "amber", .clr = amber],
         [.name = "banana", .clr = banana],
         [.name = "cadmiumYellow", .clr = cadmiumYellow],
         [.name = "lemon", .clr = lemon],
         [.name = "brass", .clr = brass],
         [.name = "armyGreen", .clr = armyGreen],
         [.name = "forestGreen", .clr = forestGreen],
         [.name = "fern", .clr = fern],
         [.name = "moss", .clr = moss],
         [.name = "avocado", .clr = avocado],
         [.name = "jade", .clr = jade],
         [.name = "emerald", .clr = emerald],
         [.name = "sage", .clr = sage],
         [.name = "pine", .clr = pine],
         [.name = "celadon", .clr = celadon],
         [.name = "sapphire", .clr = sapphire],
         [.name = "cerulean", .clr = cerulean],
         [.name = "azure", .clr = azure],
         [.name = "babyBlue", .clr = babyBlue],
         [.name = "powderBlue", .clr = powderBlue]
      ]
      
      // function :   stringToColour
      // description: convert a string to a colour constant
      // author :     @pianofire
      // arguments :
      //     colourName : string name of colour
      // returns :
      //     colourConstant (or black if not found)
      function stringToColour( colourName )
        var result = black
        var found = false
        var lowerName = ""
        var i = 0
        colourName = toLower( colourName )
        while i < len(colourDefs) and !found loop
          lowerName = toLower( colourDefs[i].name )
          if lowerName == colourName then
            result = colourDefs[i].clr
            found = true
          endif
          i += 1
        repeat
      return result
      
      // function :   colourToString
      // description: convert a colour constant to a string
      // author :     @pianofire
      // arguments :
      //     colourConstant : colour constant to convert
      // returns :
      //     colourName (or "black" if not found)
      function colourToString( colourConstant )
        var result = "black"
        var found = false
        var i = 0
        while i < len(colourDefs) and !found loop
          if colourDefs[i].clr.r == colourConstant.r and
             colourDefs[i].clr.g == colourConstant.g and
             colourDefs[i].clr.b == colourConstant.b then
            result = colourDefs[i].name
            found = true
          endif
          i += 1
        repeat
      return result
      
      // Convert a string to lower case
      function toLower( stringVar )
        var result = ""
        for i = 0 to len( stringVar ) loop
          val = chrVal( stringVar[i] )
          if val >= 65 and val <= 90 then
            val += 32
          endif
          result += chr( val )
        repeat
      return result
      
      // Unit test code by @PB____
      
      // print with new line
      function printLn( value )
      	print( value, "\n" )
      return void
      
      // join a series of values together in a string
      function join(values)
          string result = ""
          int i
          for i = 0 to len( values ) loop
      	result += str( values[ i ] )
          repeat
      return result
      
      // assert that value1 is equal to value2
      function assertEqual( value1, value2 )
          string str1 = str( value1 )
          string str2 = str( value2 )
          string outcome = ""
          if str1 == str2 then
      	ink( lime )
      	outcome = "success"
          else
      	ink( red )
      	outcome = "fail"
          endif
          println( join( [ "assert ", str1, " equals to ", str2, ": ", outcome ] ) )
      return void
      
      // print test description
      function description( text )
          ink( { 0.5, 0.5, 0.5, 1 } )
          println(text)
      return void
      
      // run unit tests with the specified delay at the end
      function runUnitTests( duration )
          var outputColour
          var outputString
          var inputString = "skyBlue"
          var inputColour = skyBlue
      
          description( "test stringToColour" ) 
          outputColour = stringToColour( inputString )
          assertEqual( outputColour, inputColour )
      
          description( "test colourToString" )   
          outputString = colourToString( inputColour )
          assertEqual( outputString, inputString )
      
          update()
          sleep( duration )
      return void
      
      runUnitTests( 10 )
      
      1 Reply Last reply Reply Quote 5
      • First post
        Last post