percentage to shade of gray - hex

I'm working on a visual data tool where I have a min and a max value. All entries will have a value that falls somewhere inbetween those. I'd like to have my bars with coloring that reflects this min and max value. Such as a 0 value would get #ffffff and a Maxed 100 would be #000000 and 50 would be #888888
So my problem is, how do I convert this? I'm not sure what the conversion would be.

Get percentage from 0 to 100
Subtract percentage from 100
Multiply value times 255
Convert to integer
Convert to two digit Hex
Concatenate Value with itself three times to get six digit hex
Profit.

Related

Understanding hex output of raw 8-bit 8000Hz PCM sine wave

Using Audacity, I generated a 1Hz Sine Wave with a 1 second length and 1.0 amplitude. This resulted in the following wave as expected.
With the Audacity sample size set to 8000Hz, I then exported the audio as RAW (header-less) Signed 8-bit PCM which resulted in an 8000 byte file (each byte is an 8-bit number between -128 and +127).
Opening up the .raw file in HxD and then setting the 'Bytes per row' to 1 and the Offset to decimal shows 8000 lines, each line showing the 8-bit number in Hex.
I can see that there are 10 0's then 10 1's then 10 2's and so on but once it goes to 16, there are 11 16's but then 10 17's and 10 18's. My question is, why are there 10 of some numbers and 11 of others?
This is just the shape of the sine wave. As you get closer to the maximum the curve is flatter so you get more equal sample values.
The left column can't be hex. It must be the sample time offset. The right column is measured value. What are the values of the right column when it's greater than 9?

How does the roll period of dygraphs work?

In this page there is the official example of the roll periods function.
What is the function used ? (given N the roll period)
With a simple moving average, the first N values should be NA, but they are not.
There are 99 values, so if I put 99 as roll period, I thought I would have a straight line, but it is not.
When I put 50 and 60, it seems that only values after the first 50 ones are changed.
Does anyone know the function, or how can I find it ?
It's a trailing average.
So if there are 100 values and you set 50 as the roll period, then:
the first value will just be the first value
the second will be the average of the first two
...
the 50th will be the average of the first 50
the 51st will be the average of values 2..51
...
the 100th will be the average of values 51..100

R calculating mean of top 10 with highest value in one bracket

So basically what i am trying to do is to calculate what the top 10 are:
df_priceusd[order(df_priceusd$pop,decreasing=T)[1:10],]
and now i want to take the prices of the top 10 and calculate the mean of the top 10´s values to get one mean value for all of the 10.
can i somehow implement this:
mean(df_priceusd$mean_priceusd)
into my other code?
or should i go at it at another angle?

Maths Range To Percentage

i need a bit of help with some maths.
I have a range 0 - 127 and i want to convert it into percentage.
So 0% = 0 and 100% = 127 and every number inbetween.
How would i do this?
Edit:
Thanks to what jon posted, i came up with:
$percent * 127 / 100
While Jon's answer was not incorrect, the answer given by belisarius was more complete, in that it allowed for a range of numbers beginning and ending with any number, and not necessarily starting with 0.
Here's a little better way to represent the formula:
percentage = (value - min) / (max - min)
If you want to represent the percentage as a whole number instead of a decimal, simply multiply the result by 100.
And here's the reverse (going from a percentage to a value):
value = ((max - min) * percentage) + min
The percentage here is a decimal. If your percentage is a whole number, simply divide it by 100 before inserting in this formula.
Generally, if you have numbers in the interval [a,b], to get the percentage inside your interval, the formula is:
percentage = 100 * (x-a) / (b-a)
Where x is your value
If you want to go from a value to a percentage, eg from 63.5 to 50%, divide your value by 127 & multiply by 100.
If you want to go the other way, eg from 50% to 63.5, it's the reverse: divide your percentage by 100 & multiply by 127.

How do I normalize an image?

If I have a series of pixels, which range from say -500 to +1000, how would I normalize all the pixels on the same gradient so that they fall between a specific range, say 0 and 255?
Some pseudocode like this would scale values linearly from one range to another
oldmin=-500
oldmax=1000
oldrange=oldmax-oldmin;
newmin=0
newmax=255;
newrange=newmax-newmin;
foreach(oldvalue)
{
//where in the old scale is this value (0...1)
scale=(oldvalue-oldmin)/oldrange;
//place this scale in the new range
newvalue=(newrange*scale)+newmin
}
Your question isn't very clear so I'm going to assume that you're doing some kind of image processing and the results you get are values from -500 to 1000 and now you need to save the color to a file where every value needs to be between 0 and 255.
How you do this is really very dependent in the application, what is really the meaning of the results and what exactly you want to do. The two main options are:
clamp the values - anything under 0 you replace by 0 and anything above 255 you replace by 255. You'll want to do this, for instance, if your image processing is some kind of interpolation which really shouldn't reach these values
Linear normalization - linearly may your minimal value to 0 and your maximal value to 255. Of course you'll first need to find the minimum and maximum. You do:
v = (origv - min)/(max - min) * 255.0
What this does is first map the values to [0,1] and then stretch them back to [0,255].
A third option is to mix and match between these two options. Your application might demand that you treat negative values as unneeded values and clamp them to 0 and positive values to linearly map to [0,255].
First make it all positive. If the minimum is -500 then add 500 to all values. Then the minimum would be 0, and the maximum would be 1500.
Then it is just a rule of three and you have it:
[value in 0,255] = 255*(Pixel/1500)
Some pseudo code may help:
foreach( pixel_value in pixel_values): # between -500 and 1000
position = (pixel_value + 500) / 1500 # gives you a 0 to 1 decimal
new_value = int(postion * 255) # or instead of casting, you could round it off
That's python code by the way.
Create two variables, MinInputValue and MaxInputValue. Initialize MinInputValue to a very large positive number (higher than the largest pixel value you ever expect to see) and MaxInputValue to a very large negative number (lower than the lowest pixel value you ever expect to see).
Loop over every pixel in the image. For each pixel, if the pixel value PixelValue is lower than MinInputValue, set MinInputValue to PixelValue. If the pixel value is higher than MaxInputValue, set MaxInputValue to PixelValue.
Create a new variable, InputValueRange, and set it to MaxInputValue - MinInputValue.
Once this is done, loop over every pixel in the image again. For each pixel PixelValue, calculate the output pixel value as 255.0 * (PixelValue - MinInputValue) / InputValueRange. You can assign this new value back to the original PixelValue, or you can set the corresponding pixel in an output image of the same size.

Resources