<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gradient &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/tag/gradient/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Wed, 30 Jun 2010 05:34:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>gradient &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Flash alpha GradientMatrix to PDF using purePDF</title>
		<link>https://blog.sephiroth.it/2010/02/10/flash-alpha-gradientmatrix-to-pdf-using-purepdf/</link>
					<comments>https://blog.sephiroth.it/2010/02/10/flash-alpha-gradientmatrix-to-pdf-using-purepdf/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Wed, 10 Feb 2010 14:41:19 +0000</pubDate>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[purePDF]]></category>
		<category><![CDATA[gradient]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[opacity]]></category>
		<category><![CDATA[pdf]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=291</guid>

					<description><![CDATA[One of the most hard thing to traslate when creating pdf documents from existing flash movies is indeed the gradient matrix. Not only because pdf and flash have 2 different coordinates system and because the gradient matrix is applied in 2 different ways, but also if you want to convert gradient with transparent colors inside. &#8230; <a href="https://blog.sephiroth.it/2010/02/10/flash-alpha-gradientmatrix-to-pdf-using-purepdf/" class="more-link">Continue reading <span class="screen-reader-text">Flash alpha GradientMatrix to PDF using purePDF</span></a>]]></description>
										<content:encoded><![CDATA[<p>One of the most hard thing to traslate when creating pdf documents from existing flash movies is indeed the <strong><a class="zem_slink freebase/en/gradient rdfa" title="Gradient" rel="ctag:means wikipedia" href="http://en.wikipedia.org/wiki/Gradient">gradient</a> matrix</strong>.</p>
<div>Not only because pdf and flash have 2 different coordinates system and because the gradient matrix is applied in 2 different ways, but also if you want to convert gradient with <strong>transparent colors</strong> inside.</div>
<div>Let me say that I&#8217;ve encountered the same issue <a href="http://www.quasimondo.com/archives/000689.php#000689">Mario posted here</a>, even if I made a little modification to his solution: <a href="/wp-content/uploads/2010/02/GradientMatrix.as">GradientMatrix.as</a></div>
<div>This is the swf example. Click on the sprite to start the animation. It will rotate and translate both the sprite and its gradient matrix, then click again to stop the animation and create the pdf file at that frame.</div>
<div id="ExampleColorGradient2">SWF HERE</div>
<div>In order to create the correct gradient matrix with the right position and rotation in <a href="http://code.google.com/p/purepdf/">purePDF</a> I&#8217;ve used the <span style="font-family: Monaco, helvetica, hirakakupro-w3, osaka, 'ms pgothic', sans-serif; font-size: 12px;">PdfShading.complexAxial<span style="font-family: arial, helvetica, hirakakupro-w3, osaka, 'ms pgothic', sans-serif; font-size: 13px;"> static method in this way:</span></span></div>
<div>
<pre>var cb_shading: PdfShading = PdfShading.complexAxial( writer, top_left.x, top_left.y, top_right.x, top_right.y, cb_colors, cb_ratios, true, true );</pre>
<p>then for the alpha masking I&#8217;ve created a PdfTransparencyGroup applied to the alpha mask.</p>
</div>
<div>You can see my solution (which probably is not the best one, but it&#8217;s the one I discovered for now) in the code below.</div>
<div>[kml_flashembed movie=&#8221;/wp-content/uploads/2010/02/ExampleColorGradient2.swf&#8221; height=&#8221;300&#8243; width=&#8221;400&#8243; /]</div>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img decoding="async" class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=d3bf64f1-e4a7-4489-aa00-664f80327378" alt="" /><span class="zem-script more-related more-info pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<p><span id="more-291"></span><br />
And this is the code I used to do that (download <a href="/wp-content/uploads/2010/02/ExampleColorGradient2.as">ExampleColorGradient2.as</a>):</p>
<pre>package wiki
{
 import flash.display.GradientType;
 import flash.display.Graphics;
 import flash.display.Sprite;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.geom.Matrix;
 import flash.geom.Point;
 import flash.geom.Rectangle;
 import flash.net.FileReference;
 import flash.text.TextField;
 import flash.text.TextFormat;
 import flash.text.TextFormatAlign;
 import flash.text.engine.ElementFormat;
 import flash.text.engine.FontDescription;
 import flash.text.engine.TextBlock;
 import flash.text.engine.TextElement;
 import flash.text.engine.TextLine;
 import flash.utils.ByteArray;

 import org.purepdf.colors.GrayColor;
 import org.purepdf.colors.RGBColor;
 import org.purepdf.pdf.PageSize;
 import org.purepdf.pdf.PdfContentByte;
 import org.purepdf.pdf.PdfDictionary;
 import org.purepdf.pdf.PdfDocument;
 import org.purepdf.pdf.PdfGState;
 import org.purepdf.pdf.PdfName;
 import org.purepdf.pdf.PdfShading;
 import org.purepdf.pdf.PdfShadingPattern;
 import org.purepdf.pdf.PdfTemplate;
 import org.purepdf.pdf.PdfTransparencyGroup;
 import org.purepdf.pdf.PdfWriter;

 [SWF(frameRate="60",width="400",height="300")]
 public class ExampleColorGradient2 extends Sprite
 {
 public function ExampleColorGradient2()
 {
 super();
 addEventListener( Event.ADDED_TO_STAGE, onAdded );
 }

 protected const SPRITE_W: Number = 250;
 protected const SPRITE_H: Number = 150;
 protected const STAGE_W: uint = 400;
 protected const STAGE_H: uint = 300;
 protected const GRADIENT_W: Number = SPRITE_W;
 protected const GRADIENT_H: Number = SPRITE_H;

 protected var sprite: Sprite;
 protected var frame: int = -1;
 protected var gradient_matrix: Matrix;
 protected var colors: Array;
 protected var alphas: Array;
 protected var ratios: Array;
 protected var gradient_rotation: Number = 0;
 protected var gradient_rect: Rectangle = new Rectangle( 0, 0, GRADIENT_W, GRADIENT_H );
 protected var text: TextField;

 protected var pair_x: Number;
 protected var pair_y: Number;

 // pdf properties
 private var cb_colors: Vector. = new Vector.();
 private var cb_ratios: Vector. = new Vector.();
 private var cb_alphas: Vector. = new Vector.();

 private var test_sprite: Sprite;

 protected function onAdded( event: Event ): void
 {
 stage.scaleMode = StageScaleMode.NO_SCALE;
 stage.align = StageAlign.TOP_LEFT;

 sprite = new Sprite();
 sprite.graphics.beginFill( 0, 0.5 );
 sprite.graphics.drawRect( 0, 0, SPRITE_W, SPRITE_H );
 sprite.graphics.endFill();

 gradient_matrix = new Matrix();
 colors = [ 0xFF0000, 0xFFFF00, 0x0000FF, 0xFF00FF, 0x00FF00 ];
 alphas = [ 1, 1, 0, 1, 1 ];
 ratios = [ 0, 63, 126, 186, 255 ];

 sprite.buttonMode = true;
 sprite.addEventListener( MouseEvent.CLICK, onClick );
 addChild( sprite );

 // pdf

 for( var k: int = 0; k < colors.length; ++k )
 {
 cb_colors[k] = RGBColor.fromARGB( 0xFFFFFFFF &#038; colors[k] );
 cb_ratios[k] = ratios[k]/255;
 cb_alphas[k] = alphas[k];
 }

 var msg_format: ElementFormat = new ElementFormat();
 msg_format.fontDescription = new FontDescription("Helvetica");
 msg_format.fontSize = 12;
 msg_format.color = 0x000000;

 var block: TextBlock = new TextBlock();
 block.content = new TextElement("Click the sprite to start/stop", msg_format);
 var message_line: TextLine = block.createTextLine();

 message_line.x = ( STAGE_W - message_line.width ) / 2;
 message_line.y = message_line.height;
 addChild( message_line );

 test_sprite = new Sprite();
 test_sprite.mouseEnabled = false;
 addChild( test_sprite );

 // background
 graphics.beginFill( 0xDDDDDD, 1 );
 for( k = 0; k < STAGE_H; k += 20 )
 {
 graphics.drawRect( 0, k, STAGE_W, 1 );
 }

 onEnterFrame( null );
 }

 protected function onEnterFrame( event: Event ): void
 {
 frame++;
 var matrix: Matrix = new Matrix();
 matrix.identity();
 matrix.translate( -SPRITE_W/2, -SPRITE_H/2 );
 matrix.rotate( radians( -frame/1.5 ) );
 matrix.translate( (STAGE_W/2) + ( 100 * Math.sin( frame/100 )), (STAGE_H/2) + ( 75 * Math.cos( frame/100 ))  );

 sprite.transform.matrix = matrix;

 gradient_rect.x = Math.sin( frame/100 ) * 100;
 gradient_rect.y = Math.cos( frame/100 ) * 100;

 var test: Matrix = new Matrix();
 test = GradientMatrix.getGradientBox( gradient_rect.width, gradient_rect.height, 0, gradient_rect.x, gradient_rect.y );
 pair_x = 1/test.a;
 pair_y = 1/test.d;

 gradient_rotation = radians( frame );
 gradient_matrix = GradientMatrix.getGradientBox( GRADIENT_W, GRADIENT_H, gradient_rotation, gradient_rect.x, gradient_rect.y );
 sprite.graphics.clear();
 sprite.graphics.beginGradientFill( GradientType.LINEAR, colors, alphas, ratios, gradient_matrix );
 sprite.graphics.drawRoundRect( 0, 0, SPRITE_W, SPRITE_H, 10, 10 );
 sprite.graphics.endFill();
 update();
 }

 protected function onClick( event: MouseEvent ): void
 {
 if( hasEventListener( Event.ENTER_FRAME ) )
 {
 removeEventListener( Event.ENTER_FRAME, onEnterFrame );
 save();
 } else {
 addEventListener( Event.ENTER_FRAME, onEnterFrame );
 }
 }

 private function save(): void
 {
 var k: int;
 var buffer: ByteArray = new ByteArray();
 var writer: PdfWriter = PdfWriter.create( buffer, PageSize.create( STAGE_W, STAGE_H ) );
 var document: PdfDocument = writer.pdfDocument;
 document.open();
 var cb: PdfContentByte = document.getDirectContent();
 cb.setTransform( new Matrix( 1, 0, 0, -1, 0, STAGE_H ) );

 // background
 cb.saveState();
 cb.setFillColor( 0xDDDDDD );
 for( k = 0; k < STAGE_H; k += 20 )
 {
 cb.rectangle( 0, k, STAGE_W, 1 );
 cb.fill();
 }
 cb.resetFill();
 cb.restoreState();

 // Exract the gradient box for pdf
 var box: Rectangle = gradient_rect.clone();
 var matrix: Matrix = new Matrix();
 matrix.translate( -GRADIENT_W/2, -GRADIENT_H/2 );
 matrix.translate( -gradient_rect.x, -gradient_rect.y );
 matrix.scale( pair_x, pair_y );
 matrix.concat( gradient_matrix );
 matrix.concat( sprite.transform.matrix );
 matrix.concat( new Matrix( 1, 0, 0, -1, 0, STAGE_H ) );

 var top_left: Point  = box.topLeft.clone();
 var top_right: Point = new Point( box.right, box.top );
 var bottom_right: Point = box.bottomRight.clone();
 var bottom_left: Point = new Point( box.left, box.bottom );

 top_left = matrix.transformPoint( top_left );
 top_right = matrix.transformPoint( top_right );
 bottom_right= matrix.transformPoint( bottom_right );
 bottom_left = matrix.transformPoint( bottom_left );

 cb.saveState();
 cb.setTransform( sprite.transform.matrix );

 // Create the template first
 var template: PdfTemplate = cb.createTemplate( GRADIENT_W, GRADIENT_H );
 var transGroup: PdfTransparencyGroup = new PdfTransparencyGroup();
 transGroup.put( PdfName.CS, PdfName.DEVICERGB );
 transGroup.isolated = true;
 transGroup.knockout = false;
 template.group = transGroup;

 var gState: PdfGState = new PdfGState();
 var maskDict: PdfDictionary = new PdfDictionary();
 maskDict.put( PdfName.TYPE, PdfName.MASK );
 maskDict.put( PdfName.S, new PdfName( "Luminosity" ) );
 maskDict.put( new PdfName( "G" ), template.indirectReference );
 gState.put( PdfName.SMASK, maskDict );
 cb.setGState( gState );

 // ALPHA TEMPLATE
 var matrix2: Matrix = new Matrix();
 matrix2.translate( -GRADIENT_W/2, -GRADIENT_H/2 );
 matrix2.scale( pair_x, pair_y );
 matrix2.concat( gradient_matrix );

 var alphas: Vector. = new Vector.( cb_alphas.length, true );
 for ( k = 0; k < cb_alphas.length; ++k )
 alphas[k] = new GrayColor( cb_alphas[k] );

 var template_shading: PdfShading = PdfShading.complexAxial( cb.writer, 0, 0, GRADIENT_W, 0, Vector.( alphas ), cb_ratios );
 var template_pattern: PdfShadingPattern = new PdfShadingPattern( template_shading );
 template_pattern.matrix = matrix2;

 template.rectangle( 0, 0, SPRITE_W, SPRITE_H );
 template.setShadingFill( template_pattern );
 template.fill();

 // CONTENT
 var cb_shading: PdfShading = PdfShading.complexAxial( writer, top_left.x, top_left.y, top_right.x, top_right.y, cb_colors, cb_ratios, true, true );
 var cb_pattern: PdfShadingPattern = new PdfShadingPattern( cb_shading );

 cb.roundRectangle( 0, 0, SPRITE_W, SPRITE_H, 5 );
 cb.setShadingFill( cb_pattern );
 cb.fill();

 cb.restoreState();
 document.close();

 var f: FileReference = new FileReference();
 f.save( buffer, "ExampleColorGradient2.pdf" );
 }

 private function update(): void
 {
 return;
 test_sprite.graphics.clear();
 test_sprite.graphics.beginFill( 0, 0.1 );
 test_sprite.graphics.drawRect( 0, 0, gradient_rect.width, gradient_rect.height );

 var matrix: Matrix = new Matrix();
 matrix.translate( -GRADIENT_W/2, -GRADIENT_H/2 );
 matrix.scale( pair_x, pair_y );

 var box: Rectangle = gradient_rect.clone();

 matrix.concat( gradient_matrix );
 matrix.concat( sprite.transform.matrix );

 test_sprite.transform.matrix = matrix;
 test_sprite.graphics.endFill();
 }

 public static function radians( degree: Number ): Number
 {
 return degree * ( Math.PI / 180 );
 }
 }
}</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2010/02/10/flash-alpha-gradientmatrix-to-pdf-using-purepdf/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
