Microsoft Small Basic

Program Listing: JGH943-1
' ASCII Art
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-06-20
' Program ID JGH943-1
' Version 0.2
'
txt = "NNN#QRRWWUZXhqSm5y7wuorzz}}>>|~\\^^^^_-`````` "
min = 51
max = 96
title = "ASCII Art 0.2"
TextWindow.Title = title
TextWindow.BackgroundColor = "White"
TextWindow.ForegroundColor = "Black"
TextWindow.Clear()
TextWindow.Write("Keyword? ")
keyword = TextWindow.Read()
GraphicsWindow.BackgroundColor = "DimGray"
url = Flickr.GetRandomPicture(keyword)
GraphicsWindow.Title = keyword + " - " + url
img = ImageList.LoadImage(url)
width = ImageList.GetWidthOfImage(img)
height = ImageList.GetHeightOfImage(img)
ch = 12
cw = 8
cols = 80
rows = 25
w = cw * cols ' text window width
h = ch * rows ' text window height
_w = w ' resized image width
_h = h ' resized image height
x = 0
y = 0
If (width / height) < (w / h) Then
_w = Math.Floor(width * h / height)
x = Math.Floor((w - _w) / 2)
ElseIf (w / h) < (width / height) Then
_h = Math.Floor(height * w / width)
y = Math.Floor((h - _h) / 2)
EndIf
GraphicsWindow.Width = w
GraphicsWindow.Height = h
GraphicsWindow.DrawResizedImage(img, x, y, _w, _h)
TextWindow.Title = title + " - " + keyword
For row = 0 To rows - 1
For col = 0 To cols - 1
ConvertCell()
EndFor
EndFor
Sub ConvertCell
' param col, row - cell to convert
x0 = col * cw
y0 = row * ch
b = 0
For y = y0 To y0 + ch - 1
For x = x0 To x0 + cw - 1
color = GraphicsWindow.GetPixel(x, y)
Color_RGBtoGray()
b = b + brightness
EndFor
EndFor
b = b / (ch * cw)
index = 1 + Math.Floor(b * (max - min))
If (col < cols - 1) Or (row < rows - 1) Then
TextWindow.Write(Text.GetSubText(txt, index, 1))
EndIf
EndSub
While "True"
Program.Delay(1000)
EndWhile
Sub Color_RGBtoGray
' Color | Convert RGB to Gray
' param color - "#rrggbb"
' return brightness - (0, 1)
' return gray - "#rrggbb"
_r = Text.GetSubText(color, 2, 2)
_g = Text.GetSubText(color, 4, 2)
_b = Text.GetSubText(color, 6, 2)
hex = _r
Math_Hex2Dec()
_r = dec
hex = _g
Math_Hex2Dec()
_g = dec
hex = _b
Math_Hex2Dec()
_b = dec
_min = Math.Min(Math.Min(_r, _g), _b)
_level = _min + Math.Round(((_r - _min) * 2 + (_g - _min) * 4 + (_b - _min) * 1 ) / 7)
brightness = Math.Round(_level / 255 * 10000) / 10000
gray = GraphicsWindow.GetColorFromRGB(_level, _level, _level)
EndSub
Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param hex
' return dec
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