views:

102

answers:

2

I have a number between 0.0 and 1.0, I would like to convert it to a grayscale color.
White = 0
Black = 1
You can show me how in any understandable language (I prefer actionscript 3)
Please, don't just give a name of a function that a language have to do this, I want to know how it does.

A: 

Hi

In Mathematica, like this

GrayLevel[0.25]

supposing your number is 0.25, of course.

Regards

Mark

High Performance Mark
Unfortunatly, I am using actionscript 3, it is not possible there :(
M28
+1  A: 

Assuming 0 as black and 1 as white, this AS3 function receives a number (from 0 to 1) and returns a hex number with a grayscale color

public static function num2gray(n:Number):Number {
    var gray:Number = n * 255;
    return(gray << 16 | gray << 8 | gray);
}
mga
Thank you so much!
M28
you could also try this for `#RRGGBB` results: function num2gray(n:Number):String { var gray:Number = n * 255; var gg:String = (gray.toString(16).length < 2) ? "0" + gray.toString(16) : gray.toString(16); return("#" + gg + "" + gg + "" + gg); }
mga