Scala, providing the shortest and most elegant solution so far ๐
def rgb(r: Int, g: Int, b: Int) = { def h(x: Int) = f"${x max 0 min 255}%02X" h(r) + h(g) + h(b) }
Tests
scala> rgb(255, 255, 255) res24: String = FFFFFF scala> rgb(255, 255, 300) res25: String = FFFFFF scala> rgb(0, 0, 0) res26: String = 000000 scala> rgb(148, 0, 211) res27: String = 9400D3 scala> rgb(-20, 275, 125) res28: String = 00FF7D scala> rgb(255, 255, 255) res29: String = FFFFFF
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Scala, providing the shortest and most elegant solution so far ๐
Tests