Microsoft Small Basic

Program Listing: HRX565-0
' Abstract and Concrete
' Version 0.2
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Program ID HRX565-0

TextWindow.Title = "Abstract and Concrete 0.2"

' average value
a = 10
b = 20
Average()
TextWindow.WriteLine("Average(a=" + a + ", b=" + b + ") = " + c)

' intermediate value
For k = 0 To 1 Step 0.25
Intermediate()
TextWindow.WriteLine("Intermediate(a=" + a + ", b=" + b + ", k=" + k + ") = " + c)
EndFor
TextWindow.WriteLine("")

' average point
pointA = "x=10;y=10;"
pointB = "x=30;y=20;"
AveragePoint()
TextWindow.Write("Average(a=(" + pointA["x"] + ", " + pointA["y"] + "), ")
TextWindow.Write("b=(" + pointB["x"] + ", " + pointB["y"] + ")) = ")
TextWindow.WriteLine("(" + pointC["x"] + ", " + pointC["y"] + ")")

' intermediate point
For k = 0 To 1 Step 0.25
IntermediatePoint()
TextWindow.Write("Intermediate(a=(" + pointA["x"] + ", " + pointA["y"] + "), ")
TextWindow.Write("b=(" + pointB["x"] + ", " + pointB["y"] + "), ")
TextWindow.Write("k=" + k + ") = ")
TextWindow.WriteLine("(" + pointC["x"] + ", " + pointC["y"] + ")")
EndFor
TextWindow.WriteLine("")

' average color
colorA = "#006699"
colorB = "#336633"
AverageColor()
TextWindow.WriteLine("Average(a=" + colorA + ", b=" + colorB + ") = " + colorC)

' intermediate color
For k = 0 To 1 Step 0.25
IntermediateColor()
TextWindow.WriteLine("Intermediate(a=" + colorA + ", b=" + colorB + ", k=" + k + ") = " + colorC)
EndFor

Sub Average
' param a - the first value
' param b - the second value
' return c - the average of the given values
c = (a + b) / 2
EndSub

Sub AveragePoint
' param pointA - the first point
' param pointB - the second point
' return pointC - the average point of the given points
n = Array.GetItemCount(pointA)
index = Array.GetAllIndices(pointA)
For j = 1 To n
a = pointA[index[j]]
b = pointB[index[j]]
Average()
pointC[index[j]] = c
EndFor
EndSub

Sub AverageColor
' param colorA - the first color
' param colorB - the second color
' return colorC - the average of the given colors
For j = 1 To 3
hex = Text.GetSubText(colorA, j * 2, 2)
Hex2Dec()
a = dec
hex = Text.GetSubText(colorB, j * 2, 2)
Hex2Dec()
b = dec
Average()
rgb[j] = c
EndFor
colorC = GraphicsWindow.GetColorFromRGB(rgb[1], rgb[2], rgb[3])
EndSub

Sub Intermediate
' param a - the first value
' param b - the second value
' param k - ratio between a and b (0 ≦ k ≦ 1)
' return c - the intermediate value between a and b
c = a * (1 - k) + b * k
EndSub

Sub IntermediatePoint
' param pointA - the first point
' param pointB - the second point
' param k - ratio between a and b (0 ≦ k ≦ 1)
' return pointC - the intermidiate point between pointA and pointB
n = Array.GetItemCount(pointA)
index = Array.GetAllIndices(pointA)
For j = 1 To n
a = pointA[index[j]]
b = pointB[index[j]]
Intermediate()
pointC[index[j]] = c
EndFor
EndSub

Sub IntermediateColor
' param a - the first color
' param b - the second color
' param k - ratio between a and b (0 ≦ k ≦ 1)
' return c - the intermediate color between colorA and colorB
For j = 1 To 3
hex = Text.GetSubText(colorA, j * 2, 2)
Hex2Dec()
a = dec
hex = Text.GetSubText(colorB, j * 2, 2)
Hex2Dec()
b = dec
Intermediate()
rgb[j] = c
EndFor
colorC = GraphicsWindow.GetColorFromRGB(rgb[1], rgb[2], rgb[3])
EndSub

Sub Hex2Dec
' param hex
' return dec
len = Text.GetLength(hex)
dec = 0
For i = 1 To len
dec = dec * 16 + Text.GetIndexOf("123456789ABCDEF", Text.GetSubText(Text.ConvertToUpperCase(hex), i, 1))
EndFor
EndSub