kRYOoX
kRYOoX

Reputation: 417

Color gradient based on int

I'm working on a small C program school project, that takes a text file filled with ints as parameter and converts the ints into x, y and z coordinates before drawing a map by connecting the points, using a library specific to the school.

The main functions are done, and among the suggested bonus functions, there's the idea of changing the color based on the altitude (the z coordinate). I can easily do so by mapping altitude values to colours, but that's tedious and not really elegant.

So here's my question : given that I know the altitude of points A and B when drawing the lines, as well as the biggest and smallest altitude values on the map, what would be the best way of getting the color value I want (represented as an int) ?

Better yet, how can I adapt this to a specific set of colours ? To be clearer, let's say I don't want to use the full spectrum of colours, but rather go through blue - yellow - green - grey - white, in order to simulate a (sort of) realistic map (water, sand, grass, mountain, snow). This, of course, is just an example.

Note : Even though this is a bonus, I'd rather try to figure out a solution with your help rather than receive a pre-written code.

Thank you.

Upvotes: 0

Views: 343

Answers (1)

ryyker
ryyker

Reputation: 23218

You can #define a limited set of discrete values, only the ones you need, and use them in your code instead of creating colors on the fly. Eg.:

#define VAL_GREEN 0x00FF00L

color generation is commonly implemented using three unsigned short int to represent RGB values - ranging from 0,0,0 to 255,255,255. Create a map of your desired color ranges (greens for grass, etc) by experimenting with various combinations of the R, G & B components. eg. 0x00, 0xFF, 0x00, would of course generate a shade of green. 0xC0, 0xC0, 0xC0 would generate a shade of grey. Look here for the MS version of color generation/mapping.

Here are some typical color definitions I use on my system: (by the way, these mappings are created using long int, and are useable on any system, Windows based or Unix based)

0xFF0000L = VAL_RED
0x00FF00L = VAL_GREEN
0x0000FFL = VAL_BLUE
0x00FFFFL = VAL_CYAN
0xFF00FFL = VAL_MAGENTA
0xFFFF00L = VAL_YELLOW
0x800000L = VAL_DK_RED
0x000080L = VAL_DK_BLUE
0x008000L = VAL_DK_GREEN
0x008080L = VAL_DK_CYAN
0x800080L = VAL_DK_MAGENTA
0x808000L = VAL_DK_YELLOW
0xC0C0C0L = VAL_LT_GRAY
0x808080L = VAL_DK_GRAY
0x000000L = VAL_BLACK
0xFFFFFFL = VAL_WHITE

0xA0A0A0L = VAL_GRAY
0xE0E0E0L = VAL_OFFWHITE

Upvotes: 1

Related Questions