Solved

Testing a value in a range

Posted on 2003-10-26
7
240 Views
Last Modified: 2012-05-04
Hi there gurus.

I am writing a program that converts numerical values into colors

For example

if value is between 0.0 and 0.01 color value is RGB(255,255,0)
                            0.011 and 0.02 color value is RGB(255,255,1)
                                        ...
                                        ...
                                        ...
                            0.091 and 0.1  color value is RGB(255,255,255)
There will be up to 1000 of these ranges
Any idea how to code this.

Regards

Peter
0
Comment
Question by:Pishky
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9622214
Many options here:

1) if...else if...else if...else...
2) Table lookup.  If this, write a quick/dirty program to generate your data tables rather then hand-code them.
3) Algorithm.  The above ranges, I assume, are for example only.  But your real situation most likely follows a pattern that can be modeled with an algorithm and implemented in code.
0
 
LVL 6

Expert Comment

by:GaryFx
ID: 9622220
If there are only a few ranges, you can just chain if-else statements.  If there are a lot, then store the RGB values in an array, and convert the ranges to integers (e.g. irange = static_cast<int>(range*100).  This second approach pays the cost of a float->int conversion, but it's much cleaner and easier to maintain, assuming your ranges are all hundredths apart.

Gary
0
 

Author Comment

by:Pishky
ID: 9622253
Thank you for the above but I am not sure that this is answering my question. The suggestions above imply a single mapping from one value to another. The problem I have is to map a range of values to a single value.

Regards

Peter
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Accepted Solution

by:
GaryFx earned 125 total points
ID: 9622265
No, they don't.  Note the multiplication by 100 in my cast:

If r is in the range [0.01, 0.02) (using the standard meaning of [ for inclusive and ) for exclusive), then r*100 will be between 1.0 and 2.0, so that static_cast<int>(r) will be 1.  That's how you get the mapping of an entire range to a single int.

Also, your ranges were (0.01, 0.02] (i.e. exclusive at the low end and inclusive at the high end).  This could be an issue, but more likely it won't matter because most of these decimals can't be represented exactly in binary anyway.

Gary
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9623131
Well

Why not simply use the integer version of the colour.

All the colours are represented by a number in the range [0, 0xFFFFFF]. If your number is in the range [0, 1) just multiply by 2^24. Then if you need to convert it to rgb it's simple.

Value = (long) (YourNumber * 0x1000000);
Red = (Value 0xFF0000) >> 16;
Green = (Value 0xFF00) >> 8;
Blue = (Value 0xFF);

If you want to convert RGB to your number then...

Value = (Red << 16) + (Green << 8) + Blue;
YourNumber = (float) Value / 0x1000000;

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546155
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: GaryFx {http:#9622265}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now