gif format

by wh0
I want to generate some 1x1 animated GIFs for making fading backgrounds.
♥0 | Line 62 | Modified 2010-12-26 05:56:25 | MIT License
play

ActionScript3 source code

/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1EGc
 */

package {
    import flash.utils.Endian;
    import flash.utils.ByteArray;
    import flash.text.TextField;
    
    import com.actionscriptbible.Example;
    import mx.utils.Base64Encoder;
    public class FlashTest extends Example {
        
        public function FlashTest() {
            var gif:ByteArray = new ByteArray();
            gif.endian = Endian.LITTLE_ENDIAN;
            
            // header
            gif.writeUTFBytes('GIF'); // signature
            gif.writeUTFBytes('89a'); // version
            
            // logical screen descriptor
            gif.writeShort(1); // logical screen width
            gif.writeShort(1); // logical screen height
            gif.writeByte(0xf6); // packed fields
                // global color table flag (immediately follows)
                // color resolution (8 bits)
                // sort flag (not ordered)
                // size of global color table (128 entries)
            gif.writeByte(0); // background color index
            gif.writeByte(0); // pixel aspect ratio
            
            // global color table
            for (var c:int = 0; c < 128; c++) {
                gif.writeByte(c * 2); // red
                gif.writeByte(c * 2); // green
                gif.writeByte(c * 2); // blue
            }
            
            // application extension
            gif.writeByte(0x21); // extension introducer
            gif.writeByte(0xff); // application extension label
            gif.writeByte(11); // block size
            gif.writeUTFBytes('NETSCAPE'); // application identifier
            gif.writeUTFBytes('2.0'); // application authentication code
            gif.writeByte(3); // length of data sub-block
            gif.writeByte(1); // 1
            gif.writeShort(0); // number of iterations
            gif.writeByte(0); // data sub-block terminator
            
            for (var f:int = 0; f < 128; f++) {
                // graphic control extension
                gif.writeByte(0x21); // extension introducer
                gif.writeByte(0xf9); // graphic control label
                gif.writeByte(4); // block size
                gif.writeByte(0x04); // packed fields
                    // reserved (0)
                    // disposal method (do not dispose)
                    // user input flag (not expected)
                    // transparent color flag (not given)
                gif.writeShort(2); // delay time
                gif.writeByte(0); // transparent color index
                gif.writeByte(0); // block terminator
                
                // image descriptor
                gif.writeByte(0x2c); // image separator
                gif.writeShort(0); // image left position
                gif.writeShort(0); // image top position
                gif.writeShort(1); // image width
                gif.writeShort(1); // image height
                gif.writeByte(0x00); // packed fields
                    // local color table flag (not present)
                    // interlace flag (not interlaced)
                    // sort flag (not ordered)
                    // size of local color table (0 entries)
                
                // table based image data
                gif.writeByte(7); // lzw minimum code size
                gif.writeByte(3); // block size
                gif.writeByte(128); // clear code
                gif.writeByte(f); // this one is actually data
                gif.writeByte(129); // end of information code
                gif.writeByte(0); // block terminator
            }
            
            // trailer
            gif.writeByte(0x3b); // GIF trailer
            
            var enc:Base64Encoder = new Base64Encoder();
            enc.insertNewLines = false;
            enc.encodeBytes(gif, 0, gif.length);
            
            var tf:TextField = new TextField();
            tf.text = 'data:image/gif;base64,' + enc.toString();
            addChild(tf);
        }
        
    }
}

Forked