Scale numbers to be <= 255?

前端 未结 13 2217
生来不讨喜
生来不讨喜 2021-02-05 18:39

I have cells for whom the numeric value can be anything between 0 and Integer.MAX_VALUE. I would like to color code these cells correspondingly.

If the val

相关标签:
13条回答
  • 2021-02-05 19:18

    The value you're looking for is: r = 255 * (value / Integer.MAX_VALUE). So you'd have to turn this into a double, then cast back to an int.

    0 讨论(0)
  • 2021-02-05 19:22

    I could just do (value / (Integer.MAX_VALUE / 255)) but that will cause many low values to be zero.

    One approach you could take is to use the modulo operator (r = value%256;). Although this wouldn't ensure that Integer.MAX_VALUE turns out as 255, it would guarantee a number between 0 and 255. It would also allow for low numbers to be distributed across the 0-255 range.

    EDIT:

    Funnily, as I test this, Integer.MAX_VALUE % 256 does result in 255 (I had originally mistakenly tested against %255, which yielded the wrong results). This seems like a pretty straight forward solution.

    0 讨论(0)
  • 2021-02-05 19:23

    Ask yourself the question, "What value should map to 128?" If the answer is about a billion (I doubt that it is) then use linear. If the answer is in the range of 10-100 thousand, then consider square root or log.

    Another answer suggested this (I can't comment or vote yet). I agree.

    r = log(value)/log(pow(2,32))*256

    0 讨论(0)
  • 2021-02-05 19:24

    If you are complaining that the low numbers are becoming zero, then you might want to normalize the values to 255 rather than the entire range of the values.

    The formula would become:

    currentValue / (max value of the set)

    0 讨论(0)
  • 2021-02-05 19:29

    This works! r= value /8421504;

    8421504 is actually the 'magic' number, which equals MAX_VALUE/255. Thus, MAX_VALUE/8421504 = 255 (and some change, but small enough integer math will get rid of it.

    if you want one that doesn't have magic numbers in it, this should work (and of equal performance, since any good compiler will replace it with the actual value:

    r= value/ (Integer.MAX_VALUE/255);

    The nice part is, this will not require any floating-point values.

    0 讨论(0)
  • 2021-02-05 19:32

    The linear implementation is discussed in most of these answers, and Artelius' answer seems to be the best. But the best formula would depend on what you are trying to achieve and the distribution of your values. Without knowing that it is difficult to give an ideal answer.

    But just to illustrate, any of these might be the best for you:

    • Linear distribution, each mapping onto a range which is 1/266th of the overall range.
    • Logarithmic distribution (skewed towards low values) which will highlight the differences in the lower magnitudes and diminish differences in the higher magnitudes
    • Reverse logarithmic distribution (skewed towards high values) which will highlight differences in the higher magnitudes and diminish differences in the lower magnitudes.
    • Normal distribution of incidence of colours, where each colour appears the same number of times as every other colour.

    Again, you need to determine what you are trying to achieve & what the data will be used for. If you have been tasked to build this then I would strongly recommend you get this clarified to ensure that it is as useful as possible - and to avoid having to redevelop it later on.

    0 讨论(0)
提交回复
热议问题