Microsoft Small Basic

Program Listing: TKZ890-0
' Chalk Colors Sun
' Version 0.4
' Copyright © 2013-2020 Nonki Takahashi, mahreen miangul. The MIT License.
'
' History:
' 0.1b 2013-03-07 Created but not implemented angle yet. (VFP128)
' 0.3 2020-06-10 Added chalk colors sun. (TKZ890)
' 0.4 2020-06-17 Rerwrote for rays for sunshine. (TKZ890-0)
'
GraphicsWindow.Title = "Chalk Colors Sun 0.4"
GraphicsWindow.width = 800
GraphicsWindow.height = 500

Colors_Init()
percent = "1=0;2=60;3=100;"
name= "1=skyblue;2=snow;3=yellow;"
GraphicsWindow.BrushColor = "Black"
For i = 1 To 3
pbox[i] = Controls.AddTextBox(10, 10 + (i - 1) * 30)
Controls.SetTextBoxText(pbox[i], percent[i])
Controls.SetSize(pbox[i], 40, 22)
nbox[i] = Controls.AddTextBox(60, 10 + (i - 1) * 30)
Controls.SetTextBoxText(nbox[i], name[i])
EndFor
Controls.AddButton("Paint", 10, 100)
param = "x=0;y=0;angle=45;"
param["width"] = GraphicsWindow.Width
param["height"] = GraphicsWindow.Height
Paint()
DrawSun()
Controls.ButtonClicked = OnButtonClicked

Sub OnButtonClicked
param = "x=0;y=0;angle=45;"
param["width"] = GraphicsWindow.Width
param["height"] = GraphicsWindow.Height
For i = 1 To 3
percent[i] = Controls.GetTextBoxText(pbox[i])
name[i] = Controls.GetTextBoxText(nbox[i])
EndFor
Paint()
DrawSun()
EndSub

Sub Paint
For i = 1 To 3
p = percent[i]
n = name[i]
param[p] = n
EndFor
FillLinearGradientRectangle()
EndSub

Sub FillLinearGradientRectangle
' param["x"], param["y"] - position of the rectangle
' param["width"], param["height"] - size of the rectangle
' param["angle"] - angle of gradient
' param["0"]..param["100"] - 0% to 100% colors
x0 = param["x"]
y0 = param["y"]
width = param["width"]
height = param["height"]
angle = param["angle"]
n = Array.GetItemCount(param)
index = Array.GetAllIndices(param)
color = ""
For i = 1 To n
If index[i] + 0 = index[i] And 0 <= index[i] And index[i] <= 100 Then
color[index[i]] = param[index[i]]
EndIf
EndFor
If color[0] = "" Then
color[0] = GraphicsWindow.BrushColor
EndIf
If color[100] = "" Then
color[100] = GraphicsWindow.BrushColor
EndIf
n = Array.GetItemCount(color)
index = Array.GetAllIndices(color)
For i = 1 To n - 1
y1 = y0 + index[i] * height / 100
y2 = y0 + index[i + 1] * height / 100
c = color[index[i]]
Color_ColorToRGB()
r1 = r
g1 = g
b1 = b
c = color[index[i + 1]]
Color_ColorToRGB()
r2 = r
g2 = g
b2 = b
For y = y1 To y2
r = Math.Floor(r1 + (r2 - r1) * (y - y1) / (y2 - y1))
g = Math.Floor(g1 + (g2 - g1) * (y - y1) / (y2 - y1))
b = Math.Floor(b1 + (b2 - b1) * (y - y1) / (y2 - y1))
GraphicsWindow.PenColor = GraphicsWindow.GetColorFromRGB(r, g, b)
GraphicsWindow.DrawLine(x0, y, x0 + width, y)
EndFor
EndFor
EndSub

Sub Color_ColorToRGB
' Color | Convert Color to RGB
' param c - color
' returns r, g, b - red, green and blue values
If Text.StartsWith(c, "#") Then
c = Text.ConvertToUpperCase(c)
Else
c = Text.ConvertToLowerCase(c)
c = colors[c]
EndIf
sHex = Text.GetSubText(c, 2, 2)
Math_Hex2Dec()
r = iDec
sHex = Text.GetSubText(c, 4, 2)
Math_Hex2Dec()
g = iDec
sHex = Text.GetSubText(c, 6, 2)
Math_Hex2Dec()
b = iDec
EndSub
Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param sHex
' returns iDec
iDec = 0
iLen = Text.GetLength(sHex)
For iPtr = 1 To iLen
iDec = iDec * 16 + Text.GetIndexOf("0123456789ABCDEF", Text.GetSubText(sHex, iPtr, 1)) - 1
EndFor
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 DrawLine
pc = GraphicsWindow.PenColor
bc = GraphicsWindow.BrushColor
GraphicsWindow.BrushColor = pc
pw = GraphicsWindow.PenWidth
x1 = param["x1"]
y1 = param["y1"]
x2 = param["x2"]
y2 = param["y2"]
len = Math.SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
For k = 0 To 1 Step pw / len / 3
x = x1 * (1 - k) + x2 * k
y = y1 * (1 - k) + y2 * k
_dx = Math.GetRandomNumber(2) - 1
_dy = Math.GetRandomNumber(2) - 1
_pw = (Math.GetRandomNumber(2) / 10 + 0.9) * pw
GraphicsWindow.FillEllipse(x - _pw / 2 + _dx, y - _pw / 2 + _dy, _pw, _pw)
EndFor
GraphicsWindow.BrushColor = bc
EndSub

Sub DrawEllipse
pc = GraphicsWindow.PenColor
bc = GraphicsWindow.BrushColor
GraphicsWindow.BrushColor = pc
pw = GraphicsWindow.PenWidth
x = param["x"]
y = param["y"]
width = param["width"]
height = param["height"]
a = width / 2
b = height / 2
len = 2 * Math.Pi * Math.Max(a, b)
For θ = 0 To 2 * Math.Pi Step pw / len * 3
_x = (x + a) + a * Math.Cos(θ)
_y = (y + b) + b * Math.Sin(θ)
_dx = Math.GetRandomNumber(2) - 1
_dy = Math.GetRandomNumber(2) - 1
_pw = (Math.GetRandomNumber(2) / 10 + 0.9) * pw
GraphicsWindow.FillEllipse(_x - _pw / 2 + _dx, _y - _pw / 2 + _dy, _pw, _pw)
EndFor
GraphicsWindow.BrushColor = bc
EndSub

Sub DrawSun
GraphicsWindow.PenWidth = 14
GraphicsWindow.penColor = "red
param = "x=260;y=100;width=380;height=330;"
DrawEllipse()
rx = 380 / 2
ry = 330 / 2
sx = 260 + rx
sy = 100 + ry
param = ""
For h = 1 To 12
_a = Math.GetRadians(h * 30)
param["x1"] = sx + 1.2 * rx * Math.Cos(_a)
param["y1"] = sy - 1.2 * ry * Math.Sin(_a)
param["x2"] = sx + 1.5 * rx * Math.Cos(_a)
param["y2"] = sy - 1.5 * ry * Math.Sin(_a)
DrawLine()
EndFor
GraphicsWindow.penColor = "navy
GraphicsWindow.drawEllipse(360, 150, 40, 60)
param = "x=360;y=150;width=40;height=60;"
DrawEllipse()
GraphicsWindow.drawEllipse(480, 150, 40, 60)
param = "x=480;y=150;width=40;height=60;"
DrawEllipse()
graphicswindow.drawellipse(420, 210, 40, 80)
param = "x=420;y=210;width=40;height=80;"
DrawEllipse()
graphicswindow.drawellipse(370, 330, 150, 40)
param = "x=370;y=330;width=150;height=40;"
DrawEllipse()
EndSub