User Guide
226 Working with Strings
This code first defines a String instance named result that will be used to build up the
ASCII art version of the bitmap image. Next, it loops through individual pixels of the source
bitmap image. Using several color-manipulation techniques (omitted here for brevity), it
converts the red, green, and blue color values of an individual pixel to a single grayscale value
(a number from 0 to 255). The code then divides that value by 4 (as shown) to convert it to a
value in the 0-63 scale, which is stored in the variable
index. (The 0-63 scale is used because
the “palette” of available ASCII characters used by this application contains 64 values.) The
palette of characters is defined as a String instance in the BitmapToAsciiConverter class:
// The characters are in order from darkest to lightest, so that their
// position (index) in the string corresponds to a relative color value
// (0 = black).
private static const palette:String =
"@#$%&8BMW*mwqpdbkhaoQ0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+_~-;,. ";
Since the index variable defines which ASCII character in the palette corresponds to the
current pixel in the bitmap image, that character is retrieved from the
palette String using
the
charAt() method. It is then appended to the result String instance using the
concatenation assignment operator (
+=). In addition, at the end of each row of pixels, a
newline character is concatenated to the end of the
result String, forcing the line to wrap to
create a new row of character “pixels.”