My favorites
▼
|
Sign in
pixelrevisionutilities
Some lightweight utilities
Project Home
Downloads
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
actionscript
/
as3
/
src
/
com
/
pixelrevision
/
effects
/
ASCIIImage.as
r74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* AS3
Copyright 2008 pixelrevision.
*/
package com.pixelrevision.effects{
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.Event;
/**
* Creates ascii text of an image
*
* @langversion ActionScript 3.0
* @playerversion Flash 9.0
*
* @author Malcolm Wilson
* @since 2008-11-09
*/
public class ASCIIImage extends Sprite {
private var _bitmapData:BitmapData;
private var _font:Font;
private var _text:String;
private var _usesColor:Boolean;
private var _sampling:uint;
private var _textField:TextField;
private var _bitmap:Bitmap;
private var _spacing:Number;
/**
* @constructor
*/
public function ASCIIImage(bitmapData:BitmapData, textField:TextField, text:String="¨¨·¸¡÷°¬ª¢î&òôÔÀÕ@Å§å®®ÑÆ", sampling:uint = 5, spacing:Number = 10, usesColor:Boolean = true){
super();
_bitmapData = bitmapData;
_text = text;
_usesColor = usesColor;
_sampling = sampling;
_textField = textField;
_textField.text = "W";
_textField.autoSize = "left";
_spacing = spacing;
var bitmapData:BitmapData = new BitmapData(1, 1, true, 0X00000000);
_bitmap = new Bitmap(bitmapData);
addChild(_bitmap);
render();
}
public function render():void{
var cols:Number = Math.floor(_bitmapData.width/_sampling);
var rows:Number = Math.floor(_bitmapData.height/_sampling);
var y:uint;
var x:uint;
_bitmap.bitmapData = new BitmapData((cols*_spacing) + _textField.width, rows*_spacing + _textField.height, true, 0x0000000);
var matrix:Matrix = new Matrix();
for(y=0; y<rows; y++){
matrix.ty = Math.floor(y*_spacing);
for(x=0; x<cols; x++){
matrix.tx = Math.floor(x*_spacing);
var c:uint = _bitmapData.getPixel(Math.floor(x*_sampling), Math.floor(y*_sampling));
// get the color of the pixel
var b:Number = getBrightness(c);
_textField.text = _text.substr(Math.floor(b*(text.length-1)), 1);
if(_usesColor){
_textField.textColor = c;
}
_bitmap.bitmapData.draw(_textField, matrix);
}
}
}
private function getBrightness(color:uint):Number{
var r:Number = (color >> 16);
var g:Number = (color & 0x00FF00) >> 8;
var b:Number = color & 0x0000FF;
// var x, val, f, i, hue, sat, val;
r /= 255;
g /= 255;
b /= 255;
return Math.max(Math.max(r, g), b);
}
public function set bitmapData(value:BitmapData):void{
_bitmapData = value;
render();
}
public function get bitmapData():BitmapData{
return _bitmapData;
}
public function set text(value:String):void{
_text = value;
render();
}
public function get text():String{
return _text;
}
public function set usesColor(value:Boolean):void{
_usesColor = value;
render();
}
public function get usesColor():Boolean{
return _usesColor;
}
public function set spacing(value:Number):void{
_spacing = value;
render();
}
public function get spacing():Number{
return _spacing;
}
public function set sampling(value:uint):void{
_sampling = value;
render();
}
public function get sampling():uint{
return _sampling;
}
}
}
Show details
Hide details
Change log
r56
by pixelrevision on Nov 9, 2008
Diff
ASCII text
Go to:
...elrevision/effects/ASCIIImage.as
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 3333 bytes, 132 lines
View raw file
Powered by
Google Project Hosting