It's a holiday in the US today, so we're flipping back through the archives to remember a classic WTF. These colors don't run, because they'll trip over their own shoelaces. Original. --Remy

Andy's company develops solutions for "Industrial" handheld devices. To make deployment and updates easier, they each run a thin client so only the server is different from project to project. This client was written by a long-gone employee in the early nineties, and had barely changed since because it "just worked". Updating it was discouraged for fear of breaking backward-compatibility.

Andy's new project was the first chance he'd had to use it, so he asked a colleague if there was some code that could be used to interface with it. What Andy received was essentially a giant method which responded to the client by cycling through a switch-statement to decide what to paint next based on the current state of the client. Andy took the initiative to create a library for making servers for these things a bit less spaghetti-like, and to encourage this new-fangled concept of code-reuse.

Another long-gone employee had written a fairly usable set of methods for interacting with it. A lot of these methods had a color parameter of type String, which was in turn sent directly to the client as a part of the command sequence. To get a hint of what this String should contain, Andy looked up some calling code and found that there was also a method which converted a standard .Net Color to a String the client would understand. Unfortunately, it looked like this:

private string translateColor(Color color) {
  if(color == System.Drawing.Color.Black) {
     return "&H0&";
  } else if(color == System.Drawing.Color.Lime) {
     return "&HFF00&";
  } else if(color == System.Drawing.Color.Red) {
     return "&HFF&";
  } else if(color == System.Drawing.Color.Yellow) {
     return "&HFFFF&";
  } else if(color == System.Drawing.Color.Orange) {
     return "&H80C0FF";
  } else if(color == System.Drawing.Color.Blue) {
     return "&HFF0000";
  } else if(color == System.Drawing.Color.LightGray) {
     return "&H808080";
  } else if(color == System.Drawing.Color.DarkGray) {
     return "&HC0C0C0";
  } else if(color == System.Drawing.Color.White) {
     return "16777215";
  } else {
     //Error - using white
     return "16777215";
  }
}

Since Andy was a diligent engineer and wanted to do things properly, he thought that there must be a way to make this more clever, and spent a good hour trying to automatically convert generic Colors into what the accepted formats seemed to be. Unfortunately none of it was understood by the client. Combining the RGB values into one integer seemed promising, but it turned out to only work for white.

Annoyed, Andy spelunked through SVN for the client, found it and looked up the code which converts the String back:

Public Shared Function GetColor(ByVal strColor As String) As Color
   Select Case strColor
      Case "&H0&"
         Return Color.Black
      Case "0"
         Return Color.Black 'color
      Case "&HFF00&"
         Return Color.Lime
      Case "65280"
         Return Color.Lime
      Case "&HFF&"
         Return Color.Red
      Case "255"
         Return Color.Red
      Case "&H8000000F"
         Return Color.LightGray
      Case "&H808080"
         Return Color.LightGray
      Case "-2147483633"
         Return Color.LightGray
      Case "&H80000005"
         Return Color.White
      Case "16777215"
         Return Color.White
      Case "&HFF0000"
         Return Color.Blue
      Case "16711680"
         Return Color.Blue
      Case "&H80C0FF"
         Return Color.Orange
      Case "65535"
         Return Color.Orange 'bright yellow
      Case "&HC0C0C0"
         Return Color.DarkGray
      Case "&HFFFF&"
         Return Color.Yellow
      Case Else
         Return Color.Purple 'error
   End Select
End Function

Andy pondered all the places from which this was called, and decided to move on.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!