Author Topic: Feature Request: Quick Value Selection  (Read 4107 times)

0 Members and 1 Guest are viewing this topic.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Feature Request: Quick Value Selection
« on: September 09, 2018, 04:11:13 pm »
If it isn't already a part of QB64, here's what I'd like to have:

Maybe I want the prog. to assign a value to a variable based on, say, the counter in a FOR...NEXT loop... 

Example: If the counter, C, runs from 1 to 3, then...

IF C = 1 THEN Main_BG_Clr& = _RGB(255, 0, 0) ELSE IF C = 2 THEN Main_BG_Clr& = _RGB(0, 255, 0) ELSE Main_BG_Clr& = _RGB(55, 55, 255)

could be replaced with...

Main_BG_Clr& = _SELVAL(C, _RGB(255, 0, 0),  _RGB(0, 255, 0)_ RGB(0, 0, 255))

...or...

ON C Main_BG_Clr& = (_RGB(255, 0, 0),  _RGB(0, 255, 0)_ RGB(0, 0, 255))

What do you think?  Maybe not enough of a time/space saver? 

I may not always finish what I've started....

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Feature Request: Quick Value Selection
« Reply #1 on: September 09, 2018, 04:49:45 pm »
There's already a number of different ways to do this. A few examples below.

Code: QB64: [Select]
  1. ' note: you should be using unsigned long integers for colors
  2.  
  3.  
  4. ' Method 1
  5.  
  6. FOR c = 1 TO 3
  7.     IF c = 1 THEN
  8.         Main_BG_Clr~& = _RGB(255, 0, 0)
  9.     ELSEIF c = 2 THEN
  10.         Main_BG_Clr~& = _RGB(0, 255, 0)
  11.     ELSE
  12.         Main_BG_Clr~& = _RGB(55, 55, 255)
  13.     END IF
  14.  
  15.  
  16. ' Method 2
  17.  
  18. FOR c = 1 TO 3
  19.     SELECT CASE c
  20.         CASE 1
  21.             Main_BG_Clr~& = _RGB(255, 0, 0)
  22.         CASE 2
  23.             Main_BG_Clr~& = _RGB(0, 255, 0)
  24.         CASE 3
  25.             Main_BG_Clr~& = _RGB(55, 55, 255)
  26.         CASE ELSE
  27.             ' anything else here
  28.     END SELECT
  29.  
  30.  
  31. ' Method 3
  32.  
  33. DIM clr~&(3)
  34.  
  35. clr~&(1) = _RGB(255, 0, 0)
  36. clr~&(2) = _RGB(0, 255, 0)
  37. clr~&(3) = _RGB(55, 55, 255)
  38.  
  39. FOR c = 1 TO 3
  40.     Main_BG_Clr~& = clr~&(c)
  41.  
  42.  
In order to understand recursion, one must first understand recursion.

FellippeHeitor

  • Guest
Re: Feature Request: Quick Value Selection
« Reply #2 on: September 09, 2018, 04:50:36 pm »
The third suggestion seems to fit SirCrow's need like a glove.
« Last Edit: September 09, 2018, 04:51:56 pm by FellippeHeitor »

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Feature Request: Quick Value Selection
« Reply #3 on: September 09, 2018, 06:25:51 pm »
I could easily have come up with numerous ways to get it done, but I wanted to see what the shortest way would be.  Yes, the third method is the way I'd choose.  Except, of course, that I always prefer not to use arrays if I can easily avoid it;  I'm always running into Duplicate definition and other errors, clumsy as I often am.

As far as unsigned LONG integers for colors, if the regular LONG works well, why is unsigned better?  Uses less memory?  After all these years, I'm still a bit foggy on a lot of things.

Thanks for your input!
I may not always finish what I've started....

FellippeHeitor

  • Guest
Re: Feature Request: Quick Value Selection
« Reply #4 on: September 09, 2018, 06:45:45 pm »
32bit colors require some pretty high numbers to be stored. Using a LONG or _UNSIGNED LONG will require the exact same amount of memory (4 bytes), but with a LONG you'll eventually get negative values when the color value overflows the limit of LONG data type variables.

In practice, for QB64 drawing functions, it makes no difference.

However, I remember Steve pointing out something to watch for in this regard, I just can't remember what it was.

Code: QB64: [Select]
  1.  
  2. a = _RGB32(255, 255, 255)
  3. b = _RGB32(255, 255, 255)
  4.  
  5.  

The code above will give you two results: -1 and 4294967295, both of which represent white, full alpha (_RGB32(255, 255, 255)).

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Feature Request: Quick Value Selection
« Reply #5 on: September 09, 2018, 06:54:08 pm »
POINT returns unsigned values for us, so SIGNED LONGS won't always match.

SCREEN _NEWIMAGE(800,600,32)
DIM White AS LONG
White = _RGB32(255,255,255)
PSET (1,1), White
PRINT White
PRINT POINT(1,1)
IF POINT(1,1) = White THEN PRINT "Match" ELSE PRINT "No Match; we glitched..."


************

Change White to an Unsigned Long and then try the same code.  ;)
« Last Edit: September 09, 2018, 06:56:09 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Feature Request: Quick Value Selection
« Reply #6 on: September 09, 2018, 07:01:05 pm »
That was that. Thanks, Steve.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Feature Request: Quick Value Selection
« Reply #7 on: September 09, 2018, 09:55:18 pm »
I guess I have some tildes to add to my code.  Thanks.
I may not always finish what I've started....