<?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>Flex Examples &#187; substr()</title>
	<atom:link href="http://blog.flexexamples.com/tag/substr/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Formatting colors as strings with ActionScript</title>
		<link>http://blog.flexexamples.com/2007/07/25/formatting-colors-as-strings-with-actionscript/</link>
		<comments>http://blog.flexexamples.com/2007/07/25/formatting-colors-as-strings-with-actionscript/#comments</comments>
		<pubDate>Thu, 26 Jul 2007 03:32:37 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[dataTipFormatFunction]]></category>
		<category><![CDATA[substr()]]></category>
		<category><![CDATA[toString()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/07/25/formatting-colors-as-strings-with-actionscript/</guid>
		<description><![CDATA[<p>Here&#8217;s a pretty weak tip, but maybe somebody out there will find it useful. Often when working with colors, you need to format the text before displaying it, otherwise blue may display as &#8220;ff&#8221; and black as &#8220;0&#8243;. Here&#8217;s a semi-handy tip for making sure numbers display as hex values with at least six characters [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a pretty weak tip, but maybe somebody out there will find it useful. Often when working with colors, you need to format the text before displaying it, otherwise blue may display as &#8220;ff&#8221; and black as &#8220;0&#8243;. Here&#8217;s a semi-handy tip for making sure numbers display as hex values with at least six characters (and all uppercase, no less).</p>
<p><span id="more-29"></span></p>
<p>The following example uses a Slider to select a number between 0&#215;000000 (black) and 0xFFFFFF (white) to use for the background color of a box. We use the slider&#8217;s tool tip to display our nicely formatted number, although we could have used a Label or anything else just as easy.</p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/07/25/formatting-colors-as-strings-with-actionscript/ --&gt;
&lt;mx:Application name="Color_format_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private function intToHex(color:int = 0):String {
                var mask:String = "000000";
                var str:String = mask + color.toString(16).toUpperCase()
                return "#" + str.substr(str.length - 6);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Box id="box"
            width="200"
            height="100"
            backgroundColor="{slider.value}" /&gt;

    &lt;mx:HSlider id="slider"
            minimum="0x000000"
            maximum="0xFFFFFF"
            liveDragging="true"
            dataTipFormatFunction="intToHex"
            width="{box.width}" /&gt;

    &lt;mx:Label text="{intToHex(slider.value)}" /&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Color_format_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Color_format_test/bin/main.html" width="100%" height="250"></iframe></p>
<p>Although quite uninteresting, this code works by taking the color as a numeric value, converting it to a hexadecimal string using <code>toString(16)</code>, converting that string to uppercase and prefixing six zeros in front of that string. Now, to make sure we only return a maximum of six alpha-numeric digits, we use the <code>substr()</code> method to just grab the end of the string. Pretty easy, huh?</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Formatting colors as strings with ActionScript on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/07/25/formatting-colors-as-strings-with-actionscript/',contentID: 'post-29',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'dataTipFormatFunction,substr(),toString()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/07/25/formatting-colors-as-strings-with-actionscript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

