Skip to content

Instantly share code, notes, and snippets.

@Guevara-chan
Last active October 26, 2017 12:30
Show Gist options
  • Save Guevara-chan/1b605e24b267fd898dc1589edc578e86 to your computer and use it in GitHub Desktop.
Save Guevara-chan/1b605e24b267fd898dc1589edc578e86 to your computer and use it in GitHub Desktop.
import System
import System.Drawing
import System.Runtime.CompilerServices
class ASCII_logo():
public shape_font = Font("Sylfaen", 20)
public fill_font = Font("Consolas", 7, FontStyle.Bold)
public text_color = Color.Pink
public bg_color = Color.Black
public noise_color = Color.FromArgb(25, 25, 25)
public text_pool = "01"
public noise_pool = "0"
public slogan = "I am error"
# --Methods goes here.
def done():
return slogan.render_text(shape_font).scan_ascii(Tuple.Create(text_pool, noise_pool))\
.render_ascii(Tuple.Create(text_color, bg_color, noise_color), fill_font)
[Extension] static def render_text(text as string, font as Font):
# Service objects preparation.
sf = StringFormat(Alignment: StringAlignment.Center, LineAlignment: StringAlignment.Center)
# Text rendering.
sizing = Graphics.FromImage(Bitmap(1, 1)).MeasureString(text, font, Point(), sf)
img = Bitmap(sizing.Width, sizing.Height)
render = Graphics.FromImage(img)
render.DrawString(text, font, SolidBrush(Color.Black), PointF(sizing.Width / 2, sizing.Height / 2), sf)
# Finalization.
return img
[Extension] static def scan_ascii(ref_img as Bitmap, char_pools as Tuple[of string, string]):
# Service objects preparation.
ascii = Text.StringBuilder(); noise = Text.StringBuilder()
ascii_gen = EndlessString(char_pools.Item1)
noise_gen = EndlessString(char_pools.Item2) if char_pools.Item2
# Reference image to ASCII conversion.
for y in range(ref_img.Height):
for x in range(ref_img.Width):
pixel_found = ref_img.GetPixel(x, y).A # Opaque -> found.
ascii.Append((ascii_gen.next() if pixel_found else " "))
noise.Append((noise_gen.next() if not pixel_found else " ")) unless noise_gen is null
noise.AppendLine() unless noise_gen is null
ascii.AppendLine()
# Finalization.
return Tuple.Create(ascii.ToString(), noise.ToString())
[Extension]
static def render_ascii(ascii as Tuple[of string,string], palette as Tuple[of Color,Color,Color], font as Font):
# Service objects preparation.
sf = StringFormat(StringFormatFlags.MeasureTrailingSpaces, Alignment: StringAlignment.Center)
# Image and render setup.
sizing = Graphics.FromImage(Bitmap(1, 1)).MeasureString(ascii.Item1, font, PointF(), sf)
img = Bitmap(sizing.Width, sizing.Height)
loc = PointF(sizing.Width / 2, 0)
render = Graphics.FromImage(img)
# Primary render.
render.Clear(palette.Item2)
render.DrawString(ascii.Item1, font, SolidBrush(palette.Item1), loc, sf)
# Additional bg noise render.
if ascii.Item2: render.DrawString(ascii.Item2, font, SolidBrush(palette.Item3), loc, sf)
# Finalization.
return img
# --Auxilary service subclass.
class EndlessString():
val as string; idx = 0
def constructor(text as string):
val = text
def next():
return val[idx = (idx+1) % val.Length]
# ==Usage sample==
ASCII_logo( slogan: "Trust is a\nweakness",
text_pool: "SOKOLOVSKIY/",
noise_pool: "",
text_color: Color.Aqua
).done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment