Microsoft Small Basic

Program Listing:
Embed this in your website
' Nearest Named Color 0.1
' Copyright (c) 2014 Nonki Takahashi.
'
' History:
' 0.1 2014-06-09 Created. ()
'
title = "Nearest Named Color 0.1"
GraphicsWindow.Title = title
Not = "True=False;False=True;"
Colors_Init()
GraphicsWindow.Width = 598
GraphicsWindow.Height = 428
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontName = "Courier New"
fs = 20
GraphicsWindow.FontSize = fs
param = "caption=Color;left=20;top=20;width=200;"
AddTextBoxWithCaption()
boxColor = return
color = "#FF9933"
Controls.SetTextBoxText(boxColor, color)
param = "color=" + color + ";left=300;top=20;width=40;height=" + (fs * 1.8) + ";"
DrawColor()
param = "caption=Name;left=20;top=60;width=200;"
AddTextBoxWithCaption()
boxNamedColor = return
NearestNamedColor()
Controls.SetTextBoxText(boxNamedColor, namedColor)
param = "color=" + namedColor + ";left=300;top=60;width=40;height=" + (fs * 1.8) + ";"
DrawColor()
btnSearch = Controls.AddButton("Search", 20, 100)
Controls.ButtonClicked = OnButtonClicked
While "True"
  If buttonClicked Then
    color = Controls.GetTextBoxText(boxColor)
    param = "color=" + color + ";left=300;top=20;width=40;height=" + (fs * 1.8) + ";"
    DrawColor()
    NearestNamedColor()
    Controls.SetTextBoxText(boxNamedColor, namedColor)
    param = "color=" + namedColor + ";left=300;top=60;width=40;height=" + (fs * 1.8) + ";"
    DrawColor()
    buttonClicked = "False"
  Else
    Program.Delay(200)
  EndIf
EndWhile
Sub AddTextBoxWithCaption
  ' param["caption"] - caption text
  ' param["left"] - left x coordinate
  ' param["top"] - top y coodinate
  ' param["width"] - width of text box
  ' return return - text box
  Stack.PushValue("local", local)
  local["caption"] = Shapes.AddText(param["caption"])
  local["fs"] = GraphicsWindow.FontSize
  Shapes.Move(local["caption"], param["left"], param["top"] + Math.Floor(local["fs"] * 0.2))
  local["dx"] = Math.Floor(local["fs"] * (Text.GetLength(param["caption"]) + 1) * 0.6)
  return = Controls.AddTextBox(param["left"] + local["dx"], param["top"])
  local["height"] = Math.Floor(local["fs"] * 1.8)
  Controls.SetSize(return, param["width"], local["height"])
  local = Stack.PopValue("local")
EndSub
Sub DrawColor
  GraphicsWindow.BrushColor = param["color"]
  GraphicsWindow.FillRectangle(param["left"], param["top"], param["width"], param["height"])
  GraphicsWindow.PenColor = "Gray"
  GraphicsWindow.DrawRectangle(param["left"], param["top"], param["width"], param["height"])
  GraphicsWindow.BrushColor = "Black"
EndSub
Sub OnButtonClicked
  buttonClicked = "True"
EndSub
Sub NearestNamedColor
  ' param color - "#rrggbb"
  ' return namedColor - named color[s]
  Color_RGBToValues()
  rgbColor = rgb
  nColor = Array.GetItemCount(colors)
  index = Array.GetAllIndices(colors)
  dmin = 256 * Math.SquareRoot(3)
  namedColor = ""
  For i = 1 To nColor
    color = colors[index[i]]
    Color_RGBToValues()
    d = Math.Power(rgb[1] - rgbColor[1], 2)
    d = d + Math.Power(rgb[2] - rgbColor[2], 2)
    d = d + Math.Power(rgb[3] - rgbColor[3], 2)
    d = Math.SquareRoot(d)
    If d = dmin Then
      namedColor = namedColor + "," + index[i]
    ElseIf d < dmin Then
      namedColor = index[i]
      dmin = d
    EndIf
  EndFor
EndSub
Sub Color_NameToRGB
  ' Color | Convert Color to RGB
  ' param color - color name
  ' returns color -"#rrggbb"
  If Text.StartsWith(color, "#") Then
    If Text.GetLength(color) = 9 Then
      color = "#" + Text.GetSubText(color, 4, 6)
    EndIf
    If Text.GetLength(color) = 7 Then
      color = Text.ConvertToUpperCase(color)
    Else
      color = "#000000"
    EndIf
  Else
    color = Text.ConvertToLowerCase(color)
    color = colors[color]
    If color = "" Then
      color = "#000000"
    EndIf
  EndIf
EndSub
Sub Color_RGBToValues
  ' param color - "#rrggbb" or color name
  ' return rgb[] - 0..255 for each red, green, and blue
  red = Text.GetSubText(color, 2, 2)
  green = Text.GetSubText(color, 4, 2)
  blue = Text.GetSubText(color, 6, 2)
  hex = red
  Math_Hex2Dec()
  rgb[1] = dec
  hex = green
  Math_Hex2Dec()
  rgb[2] = dec
  hex = blue
  Math_Hex2Dec()
  rgb[3] = dec
EndSub
Sub Color_RGBtoGray
  ' Color | Convert RGB to Gray
  ' param color - "#rrggbb"
  ' return brightness - (0, 1)
  ' return gray - "#rrggbb"
  Color_RGBToValues()
  min = Math.Min(Math.Min(rgb[1], rgb[2]), rgb[3])
  level = min + Math.Round(((rgb[1] - min) * 2 + (rgb[2] - min) * 4 + (rgb[2] - min) * 1 ) / 7)
  brightness = Math.Round(level / 255 * 10000) / 10000
  gray = GraphicsWindow.GetColorFromRGB(level, level, level)
EndSub
Sub Colors_Init
  colors["aliceblue"]="#F0F8FF"
  colors["antiquewhite"]="#FAEBD7"
  colors["aqua"]="#00FFFF"
  colors["aquamarine"]="#7FFFD4"
  colors["azure"]="#F0FFFF"
  colors["beige"]="#F5F5DC"
  colors["bisque"]="#FFE4C4"
  colors["black"]="#000000"
  colors["blanchedalmond"]="#FFEBCD"
  colors["blue"]="#0000FF"
  colors["blueviolet"]="#8A2BE2"
  colors["brown"]="#A52A2A"
  colors["burlywood"]="#DEB887"
  colors["cadetblue"]="#5F9EA0"
  colors["chartreuse"]="#7FFF00"
  colors["chocolate"]="#D2691E"
  colors["coral"]="#FF7F50"
  colors["cornflowerblue"]="#6495ED"
  colors["cornsilk"]="#FFF8DC"
  colors["crimson"]="#DC143C"
  colors["cyan"]="#00FFFF"
  colors["darkblue"]="#00008B"
  colors["darkcyan"]="#008B8B"
  colors["darkgoldenrod"]="#B8860B"
  colors["darkgray"]="#A9A9A9"
  colors["darkgreen"]="#006400"
  colors["darkkhaki"]="#BDB76B"
  colors["darkmagenta"]="#8B008B"
  colors["darkolivegreen"]="#556B2F"
  colors["darkorange"]="#FF8C00"
  colors["darkorchid"]="#9932CC"
  colors["darkred"]="#8B0000"
  colors["darksalmon"]="#E9967A"
  colors["darkseagreen"]="#8FBC8F"
  colors["darkslateblue"]="#483D8B"
  colors["darkslategray"]="#2F4F4F"
  colors["darkturquoise"]="#00CED1"
  colors["darkviolet"]="#9400D3"
  colors["deeppink"]="#FF1493"
  colors["deepskyblue"]="#00BFFF"
  colors["dimgray"]="#696969"
  colors["dodgerblue"]="#1E90FF"
  colors["firebrick"]="#B22222"
  colors["floralwhite"]="#FFFAF0"
  colors["forestgreen"]="#228B22"
  colors["fuchsia"]="#FF00FF"
  colors["gainsboro"]="#DCDCDC"
  colors["ghostwhite"]="#F8F8FF"
  colors["gold"]="#FFD700"
  colors["goldenrod"]="#DAA520"
  colors["gray"]="#808080"
  colors["green"]="#008000"
  colors["greenyellow"]="#ADFF2F"
  colors["honeydew"]="#F0FFF0"
  colors["hotpink"]="#FF69B4"
  colors["indianred"]="#CD5C5C"
  colors["indigo"]="#4B0082"
  colors["ivory"]="#FFFFF0"
  colors["khaki"]="#F0E68C"
  colors["lavender"]="#E6E6FA"
  colors["lavenderblush"]="#FFF0F5"
  colors["lawngreen"]="#7CFC00"
  colors["lemonchiffon"]="#FFFACD"
  colors["lightblue"]="#ADD8E6"
  colors["lightcoral"]="#F08080"
  colors["lightcyan"]="#E0FFFF"
  colors["lightgoldenrodyellow"]="#FAFAD2"
  colors["lightgray"]="#D3D3D3"
  colors["lightgreen"]="#90EE90"
  colors["lightpink"]="#FFB6C1"
  colors["lightsalmon"]="#FFA07A"
  colors["lightseagreen"]="#20B2AA"
  colors["lightskyblue"]="#87CEFA"
  colors["lightslategray"]="#778899"
  colors["lightsteelblue"]="#B0C4DE"
  colors["lightyellow"]="#FFFFE0"
  colors["lime"]="#00FF00"
  colors["limegreen"]="#32CD32"
  colors["linen"]="#FAF0E6"
  colors["magenta"]="#FF00FF"
  colors["maroon"]="#800000"
  colors["mediumaquamarine"]="#66CDAA"
  colors["mediumblue"]="#0000CD"
  colors["mediumorchid"]="#BA55D3"
  colors["mediumpurple"]="#9370DB"
  colors["mediumseagreen"]="#3CB371"
  colors["mediumslateblue"]="#7B68EE"
  colors["mediumspringgreen"]="#00FA9A"
  colors["mediumturquoise"]="#48D1CC"
  colors["mediumvioletred"]="#C71585"
  colors["midnightblue"]="#191970"
  colors["mintcream"]="#F5FFFA"
  colors["mistyrose"]="#FFE4E1"
  colors["moccasin"]="#FFE4B5"
  colors["navajowhite"]="#FFDEAD"
  colors["navy"]="#000080"
  colors["oldlace"]="#FDF5E6"
  colors["olive"]="#808000"
  colors["olivedrab"]="#6B8E23"
  colors["orange"]="#FFA500"
  colors["orangered"]="#FF4500"
  colors["orchid"]="#DA70D6"
  colors["palegoldenrod"]="#EEE8AA"
  colors["palegreen"]="#98FB98"
  colors["paleturquoise"]="#AFEEEE"
  colors["palevioletred"]="#DB7093"
  colors["papayawhip"]="#FFEFD5"
  colors["peachpuff"]="#FFDAB9"
  colors["peru"]="#CD853F"
  colors["pink"]="#FFC0CB"
  colors["plum"]="#DDA0DD"
  colors["powderblue"]="#B0E0E6"
  colors["purple"]="#800080"
  colors["red"]="#FF0000"
  colors["rosybrown"]="#BC8F8F"
  colors["royalblue"]="#4169E1"
  colors["saddlebrown"]="#8B4513"
  colors["salmon"]="#FA8072"
  colors["sandybrown"]="#F4A460"
  colors["seagreen"]="#2E8B57"
  colors["seashell"]="#FFF5EE"
  colors["sienna"]="#A0522D"
  colors["silver"]="#C0C0C0"
  colors["skyblue"]="#87CEEB"
  colors["slateblue"]="#6A5ACD"
  colors["slategray"]="#708090"
  colors["snow"]="#FFFAFA"
  colors["springgreen"]="#00FF7F"
  colors["steelblue"]="#4682B4"
  colors["tan"]="#D2B48C"
  colors["teal"]="#008080"
  colors["thistle"]="#D8BFD8"
  colors["tomato"]="#FF6347"
  colors["turquoise"]="#40E0D0"
  colors["violet"]="#EE82EE"
  colors["wheat"]="#F5DEB3"
  colors["white"]="#FFFFFF"
  colors["whitesmoke"]="#F5F5F5"
  colors["yellow"]="#FFFF00"
  colors["yellowgreen"]="#9ACD32"
EndSub
Sub Math_Hex2Dec
  ' Math | Convert hexadecimal to decimal
  ' param hex - hexadecimal
  ' return dec - decimal
  dec = 0
  len = Text.GetLength(hex)
  For ptr = 1 To len
    dec = dec * 16 + Text.GetIndexOf("0123456789ABCDEF", Text.GetSubText(hex, ptr, 1)) - 1
  EndFor
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.