Microsoft Small Basic

Program Listing: TLF750
' Simple Color Slider
' Copyright © 2018 Nonki Takahashi. The MIT License
' Last update 2018-01-21

GraphicsWindow.Title = "Simple Color Slider"
Init()
While "True"
GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB(r, g, b)
color = GraphicsWindow.BackgroundColor
buttonClicked = "False"
While Not[buttonClicked]
Program.Delay(200)
EndWhile
ColorSlider()
EndWhile

Sub AddSlider
' param r, g, b - color to add
c[1] = r
c[2] = g
c[3] = b
GraphicsWindow.PenWidth = 0
For i = 1 To 3
GraphicsWindow.BrushColor = rgb[i]
colorBar[i] = Shapes.AddRectangle(85, 255)
Shapes.Move(colorBar[i], x + (i - 1) * 85, y)
EndFor
UpdateSlider()
EndSub

Sub CheckSlider
si = 0
For i = 1 To 3
x1 = Shapes.GetLeft(slider[i])
y1 = Shapes.GetTop(slider[i])
x2 = x1 + 20
y2 = y1 + 20
If (x1 <= dx) And (dx <= x2) And (y1 <= dy) And (dy <= y2) Then
ox = dx - x1
oy = dy - y1
si = i
i = 3 ' exit For
EndIf
EndFor
If si = 0 Then
mouseDown = "False"
EndIf
EndSub

Sub ColorSlider
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "#CC000000"
mask = Shapes.AddRectangle(gw, gh)
Color_ColorToRGB()
x = Math.Floor((gw - (255 * 2 + gap)) / 2)
y = Math.Floor((gh - 255) / 2)
AddSlider()
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = color
colorSample = Shapes.AddRectangle(255, 255)
Shapes.Move(colorSample, x + 255 + gap, y)
GraphicsWindow.BrushColor = "Black"
ok = Controls.AddButton("OK", gw - 100, gh - 40)
cancel = Controls.AddButton("Cancel", gw - 60, gh - 40)
buttonClicked = "False"
While Not[buttonClicked]
If mouseDown Then
mdHalt = "True"
CheckSlider()
mdHalt = "False"
While mouseDown
If mouseMove Then
mmHalt = "True"
c[si] = y + 254 - (my - oy + 10)
If c[si] < 0 Then
c[si] = 0
ElseIf 255 < c[si] Then
c[si] = 255
EndIf
UpdateSlider()
color = GraphicsWindow.GetColorFromRGB(c[1], c[2], c[3])
GraphicsWindow.BrushColor = color
Shapes.Remove(colorSample)
colorSample = Shapes.AddRectangle(255, 255)
Shapes.Move(colorSample, x + 255 + gap, y)
mouseMove = "False"
mmHalt = "False"
Else
Program.Delay(100)
EndIf
EndWhile
Else
Program.Delay(100)
EndIf
EndWhile
If Controls.LastClickedButton = ok Then
r = c[1]
g = c[2]
b = c[3]
EndIf
RemoveSlider()
Shapes.Remove(colorSample)
Shapes.Remove(mask)
EndSub

Sub Init
Not = "False=True;True=False;"
rgb = "1=Red;2=Lime;3=Blue;"
gw = 598
gh = 428
gap = 30
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "Black"
btn = Controls.AddButton("Color", 10, 10)
r = Math.GetRandomNumber(256) - 1
g = Math.GetRandomNumber(256) - 1
b = Math.GetRandomNumber(256) - 1
mdHalt = "False"
mmHalt = "False"
Controls.ButtonClicked = OnButtonClicked
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseUp = OnMouseUp
EndSub

Sub OnButtonClicked
buttonClicked = "True"
EndSub

Sub OnMouseDown
If Not[mdHalt] Then
mouseDown = "True"
dx = GraphicsWindow.MouseX
dy = GraphicsWindow.MouseY
EndIf
EndSub

Sub OnMouseMove
If Not[mmHalt] Then
mouseMove = "True"
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
EndIf
EndSub

Sub OnMouseUp
mouseDown = "False"
EndSub

Sub RemoveSlider
For i = 1 To 3
Shapes.Remove(colorBar[i])
Shapes.Remove(black[i])
Shapes.Remove(slider[i])
EndFor
Shapes.Remove(white)
Controls.Remove(ok)
Controls.Remove(cancel)
EndSub

Sub UpdateSlider
GraphicsWindow.PenWidth = 0
min = 255
For i = 1 To 3
If c[i] < min Then
min = c[i]
EndIf
GraphicsWindow.BrushColor = "Black"
Shapes.Remove(black[i])
If c[i] < 255 Then
black[i] = Shapes.AddRectangle(85, 255 - c[i])
Shapes.Move(black[i], x + (i - 1) * 85, y)
EndIf
EndFor
Shapes.Remove(white)
If 0 < min Then
GraphicsWindow.BrushColor = "White"
white = Shapes.AddRectangle(255, min)
Shapes.Move(white, x, y + 255 - min)
EndIf
GraphicsWindow.PenWidth = 1
For i = 1 To 3
GraphicsWindow.PenColor = "Gray"
GraphicsWindow.BrushColor = "LightGray"
Shapes.Remove(slider[i])
slider[i] = Shapes.AddRectangle(21, 21)
Shapes.Move(slider[i], x + (i - 1) * 85 + (85 - 21) / 2, y + 254 - c[i] - 10)
EndFor
EndSub

Sub Color_ColorToRGB
' Color | Convert color to RGB
' param color - to convert
' returns r, g, b - red, green and blue values
If Text.StartsWith(color, "#") Then
c = Text.ConvertToUpperCase(color)
Else
Stack.PushValue("local", GraphicsWindow.PenColor)
GraphicsWindow.PenColor = color
c = GraphicsWindow.PenColor
GraphicsWindow.PenColor = Stack.PopValue("local")
EndIf
hex = Text.GetSubText(c, 2, 2)
Math_Hex2Dec()
r = dec
hex = Text.GetSubText(c, 4, 2)
Math_Hex2Dec()
g = dec
hex = Text.GetSubText(c, 6, 2)
Math_Hex2Dec()
b = dec
EndSub

Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param hex
' returns dec
dec = 0
len = Text.GetLength(hex)
For p = 1 To len
dec = dec * 16 + Text.GetIndexOf("123456789ABCDEF", Text.GetSubText(hex, p, 1))
EndFor
EndSub