?
Solved

Testing a value in a range

Posted on 2003-10-26
7
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
GaryFx earned 375 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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