Scale a known range of numbers into another range

Sometimes in programming it is needed to scale a known number range into another range. I needed this for an exercise in Data and Information Visualization course. I knew the range of numbers I had scale into, a range between 0 – 1020 to implement a color temperature for my visualization.

Programmer’s best friend stackoverflow helped to figure out how this can be done. Alias ‘ irritate’ breaks the problem down into algebraic concept in his great reply.

The implementation in Java as follows:

// Scaling between 0 - 1020
int value = (int) ((((COLOR_MAX - COLOR_MIN) * (this.power - min)) / (max - min)) + COLOR_MIN);

COLOR_MIN and COLOR_MAX holds the range that we are scaling to (0 and 1020) , min and max are the maximum and minimum values of the original range and this.power is the value that we are translating.

Hopefully this help if anyone is struggling with the same problem.