Microsoft Small Basic

Program Listing: QGV368
' Small Basic Logo
' Logo Copyright © 2016 Microsoft.
' Program Copyright © 2016 Nonki Takahashi. The MIT License.
' Program ID

GraphicsWindow.Title = "Small Basic Logo"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
While "True"
DrawLogo()
Program.Delay(5000)
GraphicsWindow.Clear()
EndWhile

Sub DrawLogo
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Black"
mask = Shapes.AddRectangle(gw , gh)
Shapes.SetOpacity(mask, 0)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillEllipse(120, 150, 100, 100)
param = "x=120;y=150;size=50;color=#F4501F;" ' Orange
AddSquare()
param = "x=170;y=200;size=50;color=#FCB901;" ' Yellow
AddSquare()
param = "x=170;y=150;size=50;color=#7CB70A;" ' Green
AddSquare()
param = "x=120;y=200;size=50;color=#03A3ED;" ' Blue
AddSquare()
GraphicsWindow.BrushColor = "White"
n = 8 ' octagon
x1 = 170 ' center
y1 = 200
r = 32 ' circumradius
x2 = x1 ' top vertex
y2 = y1 - r
RegularPolygon()
GraphicsWindow.BrushColor = "DimGray"
r = 28 ' circumradius
y2 = y1 - r
RegularPolygon()
Program.Delay(500)
GraphicsWindow.BrushColor = "White"
r = 21 ' circumradius
y2 = y1 - r
RegularPolygon()
GraphicsWindow.PenColor = "DimGray"
GraphicsWindow.PenWidth = 6
GraphicsWindow.DrawLine(x1 - 24, y1, x1 + 24, y1)
GraphicsWindow.PenWidth = 16
GraphicsWindow.DrawLine(x1, y1 - 24, x1, y1 + 24)
Program.Delay(500)
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenWidth = 4
GraphicsWindow.DrawLine(x1 - 15, y1 - 5, x1, y1 - 5)
GraphicsWindow.DrawLine(x1 - 28, y1 + 5, x1 - 15, y1 + 5)
GraphicsWindow.DrawLine(x1, y1 - 50, x1, y1 + 50)
GraphicsWindow.DrawLine(x1 - 50, y1, x1 - 30, y1)
GraphicsWindow.DrawLine(x1 + 30, y1, x1 + 50, y1)
r2 = 30
r3 = 52
r4 = 440
For a = 0 To 315 Step 45
_a = Math.GetRadians(a)
x2 = x1 + r2 * Math.Sin(_a)
y2 = y1 + r2 * Math.Cos(_a)
x3 = x1 + r3 * Math.Sin(_a)
y3 = y1 + r3 * Math.Cos(_a)
x4 = x1 + r4 * Math.Sin(_a)
y4 = y1 + r4 * Math.Cos(_a)
If Math.Remainder(a, 90) <> 0 Then
GraphicsWindow.DrawLine(x2, y2, x3, y3)
EndIf
light[a] = Shapes.AddLine(x1, y1, x4, y4)
EndFor
GraphicsWindow.FontBold = "False"
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.FontName = "Segoe UI Semibold"
GraphicsWindow.FontSize = 21
GraphicsWindow.DrawText(230, 160, "Microsoft")
GraphicsWindow.FontSize = 10
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.DrawText(230, 230, "Every Kid Can Code")
GraphicsWindow.BrushColor = "#F4501F"
GraphicsWindow.FontName = "Segoe Print"
GraphicsWindow.FontSize = 40
GraphicsWindow.DrawText(230, 170, "Small Basic")
GraphicsWindow.PenWidth = 3
GraphicsWindow.PenColor = "#FCB901"
GraphicsWindow.DrawLine(230, 227, 460, 227)
For op = 80 To 0 Step -1
Shapes.SetOpacity(mask, op)
Program.Delay(10)
EndFor
For op = 100 To 0 Step -1
For a = 0 To 315 Step 45
Shapes.SetOpacity(light[a], op)
EndFor
Program.Delay(5)
EndFor
EndSub

Sub AddSquare
width = param["size"]
x = param["x"]
addColor = param["color"]
For y = param["y"] To param["y"] + width - 1
GetLine()
AddLine()
EndFor
EndSub

Sub GetLine
' param x, y - left end position of the line
' param width - of the line
' return seg - segment array for the line
seg = ""
iSeg = 0
For _x = x To x + width - 1
color = GraphicsWindow.GetPixel(_x, y)
If seg[iSeg]["color"] <> color Then
iSeg = iSeg + 1
seg[iSeg]["color"] = color
seg[iSeg]["x"] = _x
seg[iSeg]["y"] = y
seg[iSeg]["width"] = 1
Else
seg[iSeg]["width"] = seg[iSeg]["width"] + 1
EndIf
EndFor
EndSub

Sub AddLine
' param seg - segment array for a line
' param subColor - color to subtract
nSeg = Array.GetItemCount(seg)
index = Array.GetAllIndices(seg)
color2 = addColor
For iSeg = 1 To nSeg
color1 = seg[iSeg]["color"]
Color_Add()
GraphicsWindow.BrushColor = color
GraphicsWindow.FillRectangle(seg[iSeg]["x"], seg[iSeg]["y"], seg[iSeg]["width"], 1)
EndFor
EndSub

Sub RegularPolygon
For i = 1 To n
a = 2 * Math.Pi * i / n
x3 = x1 + r * Math.Sin(a)
y3 = y1 - r * Math.Cos(a)
GraphicsWindow.FillTriangle(x1, y1, x2, y2, x3, y3) ' fill
'GraphicsWindow.DrawLine(x2, y2, x3, y3) ' draw
x2 = x3
y2 = y3
EndFor
EndSub

Sub Color_Add
' param color1, color2 - for color addition
' return color - result
color = color1
Color_RGBToValues()
rgb1 = rgb
color = color2
Color_RGBToValues()
rgb2 = rgb
For i = 1 To 3
rgb[i] = Math.Min(rgb1[i] + rgb2[i], 255)
EndFor
color = GraphicsWindow.GetColorFromRGB(rgb[1], rgb[2], rgb[3])
EndSub

Sub Color_RGBToValues
' param color - "#rrggbb"
' return rgb[] - 0..255 for each red, green, and blue
hex = Text.GetSubText(color, 2, 2)
Math_Hex2Dec()
rgb[1] = dec
hex = Text.GetSubText(color, 4, 2)
Math_Hex2Dec()
rgb[2] = dec
hex = Text.GetSubText(color, 6, 2)
Math_Hex2Dec()
rgb[3] = dec
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("123456789ABCDEF", Text.GetSubText(hex, ptr, 1))
EndFor
EndSub