{"id":431,"date":"2009-10-27T09:50:16","date_gmt":"2009-10-27T16:50:16","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2329"},"modified":"2009-10-27T09:50:16","modified_gmt":"2009-10-27T16:50:16","slug":"three-color-triangle-2","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=431","title":{"rendered":"Three Color Triangle"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/*\r\n*       Petri Leskinen, Finland\r\n*       25 October 2009\r\n*       pixelero.wordpress.com\r\n*\r\n*       Actionscript 3.0, Flash CS3, Flash Player 10\r\n*\r\n*       threeColorTriangle\r\n*       draws a triangle with a gradient fill of three colors for each vertex\r\n*       using graphics.drawTriangles and a BitmapData of size 2x2\r\n*\/\r\nfunction threeColorTriangle(\r\n      point0:Point, color0:uint,\r\n      point1:Point, color1:uint,\r\n      point2:Point, color2:uint):void {\r\n\r\n      \/\/      create a  bitmap of size 2x2 pixels\r\n      var bmd:BitmapData = new BitmapData(2,2,true);\r\n      \/\/      copy colors to bitmap\r\n      \/\/      the fourth color is average of color1 and color2\r\n      bmd.setVector(bmd.rect,\r\n              Vector.&lt;uint&gt;(&#x5B;color0,color1,color2,\r\n                                         (color1+color2)&gt;&gt;1\r\n                                         ]));\r\n\r\n      \/\/      draw triangle\r\n      this.graphics.beginBitmapFill(bmd,null,false,true \/* =smooth *\/ );\r\n      this.graphics.drawTriangles(\r\n              \/\/ x,y -coordinates\r\n              Vector.&lt;Number&gt;(&#x5B;\r\n                      point0.x,point0.y,\r\n                      point1.x,point1.y,\r\n                      point2.x,point2.y]),\r\n              \/\/ indices\r\n              Vector.&lt;int&gt;(&#x5B;0,1,2]),\r\n              \/\/ texture coordinates\r\n              Vector.&lt;Number&gt;(&#x5B;0,0, 1,0, 0,1])\r\n              );\r\n}\r\n\r\n\r\n\/\/ demo, let&#039;s draw some of these on the stage\r\nrandomize();\r\nfunction randomize():void {\r\n      this.graphics.clear();\r\n\r\n      for (var i:int = 0;i&lt;128;i++) {\r\n              \/\/      pick some random colors\r\n              var color0:uint = 0xFFFFFFFF;\r\n              var color1:uint = 0xFFFFFF*Math.random() | 0xFF000000;\r\n              var color2:uint = 0xFFFFFF*Math.random() | 0xFF000000;\r\n\r\n              \/\/      random points \r\nvar point0:Point = new Point(Math.random()*stage.stageWidth,\r\n            Math.random()*stage.stageHeight);\r\nvar point1:Point = new Point(point0.x+200*(Math.random()-Math.random()),\r\n            point0.y+200*(Math.random()-Math.random()));\r\nvar point2:Point = new Point(point0.x+200*(Math.random()-Math.random()),\r\n            point0.y+200*(Math.random()-Math.random()));\r\n\r\n        threeColorTriangle(point0, color0, point1, color1, point2,  color2);\r\n      }\r\n}\r\n<\/pre>\n<p>This snippet is by <a href=\"http:\/\/pixelero.wordpress.com\/\">Petri Leskinen (pixelero)<\/a>. It draws a triangle with a gradient fill of three colors for each vertex using Graphics.drawTriangles and a BitmapData of size 2&#215;2. This is the simplest case, it can easily be extended to four colors or maybe to use a larger 3&#215;3, 4&#215;4 etc&#8230; bitmap.<\/p>\n<p>Here&#8217;s a still&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/triangles.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* * Petri Leskinen, Finland * 25 October 2009 * pixelero.wordpress.com * * Actionscript 3.0, Flash CS3, Flash Player 10 * * threeColorTriangle * draws a triangle with a gradient fill of three colors for each vertex * using graphics.drawTriangles and a BitmapData of size 2&#215;2 *\/ function threeColorTriangle( point0:Point, color0:uint, point1:Point, color1:uint, point2:Point, color2:uint):void [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,79],"tags":[3,14,135,4],"class_list":["post-431","post","type-post","status-publish","format-standard","hentry","category-graphics","category-vector","tag-actionscript","tag-as3","tag-contest","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=431"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}