Author Topic: Steve's Quick Lesson on Number TYPEs and Colors  (Read 3392 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Steve's Quick Lesson on Number TYPEs and Colors
« on: December 14, 2021, 12:53:47 am »
Code: QB64: [Select]
  1. Screen _NewImage(640, 640, 32)
  2. _Title "Number Types and Colors"
  3. Print "Welcome to Steve's Qucik Lesson on Number Types and Colors."
  4. Print "The most important thing to keep in mind in this lesson is that we're going to be talking exclusively about 32-bit color values here.  For all other screen modes, this lesson holds much less importance."
  5. Print "Press <ANY KEY> to begin!"
  6. Cls , 0
  7. Print "First, let's talk about how SINGLE variable types work (or DON'T work), in regards to 32-bit colors."
  8. Print "Let's choose a nice color and use it to draw a box on the screen."
  9. Print "How about we choose a BLUE box?  _RGB32(0, 0, 255)"
  10. Line (50, 90)-(250, 250), _RGB32(0, 0, 255), BF
  11. Locate 18, 1: Print "Looks like a nice BLUE box.  Right?"
  12. Print "Press <ANY KEY> to continue!"
  13. Cls , 0
  14. Print "Now, let's store that BLUE value inside a SINGLE tyoe variable."
  15. Print "BLUE = _RGB32(0, 0, 255)"
  16. Print "Once we've did that, let's draw the exact same box on the screen again with the variable."
  17. BLUE = _RGB32(0, 0, 256)
  18. Line (50, 90)-(250, 250), BLUE, BF
  19. Locate 18, 1: Print "Looks like a nice BLUE box.  Right?"
  20. Print "Press <ANY KEY> to continue!"
  21. Cls , 0
  22. Print "What do you guys mean, 'What box?'??"
  23. Print "Do you mean to tell me you nice folks DIDN'T see a pretty BLUE box on the last screen??"
  24. Print "Just what the hell happened to it?!!"
  25. Print "For the answer to that, let's print out two values to the screen:"
  26. Print "BLUE = "; BLUE
  27. Print "_RGB32(0, 0, 255) = "; _RGB32(0, 0, 255)
  28. Print "At first glance, those APPEAR to be the same numbers, but let's expand the      scientific notation fully:"
  29. Blue&& = BLUE
  30. Print "BLUE = "; Blue&&
  31. Print "_RGB32(0, 0, 255) = "; _RGB32(0, 0, 255)
  32. Print "Press <ANY KEY> to continue!"
  33. Cls , 0
  34. Print "HOLY COW, BATMAN!!  Was those two numbers DIFFERENT?!!"
  35. Print "BLUE = "; Blue&&; "vs"; _RGB32(0, 0, 255)
  36. Print "Well... They're only a LITTLE different...  Right?"
  37. Print "I mean, how bad can one little number difference be?  Right??"
  38. Print "For the answer to that, let's look at the HEX values of those numbers:"
  39. Print "BLUE = "; Hex$(Blue&&)
  40. Print "_RGB32(0, 0, 255) - "; Hex$(_RGB32(0, 0, 255))
  41. Print "And to help understand what we're seeing in HEX, break those values down into   groups of 2 in your mind."
  42. Print "(I'm too lazy to do it for you..)"
  43. Print "The first two values are ALPHA, followed by RED, followed by GREEN, followed by BLUE."
  44. Print "So  BLUE = FF alpha, 00 red 01 green, 00 blue"
  45. Print "_RGB32(0, 0, 0) = FF alpha, 00 red, 00 green, FF blue"
  46. Print "And keep in mine that FF is HEX for the decimal value of 255."
  47. Print "Press <ANY KEY> to continue!"
  48. Cls , 0
  49. Print "Since SINGLE values lose precision after numbers get so large, our variable BLUE"
  50. Print "has to round to the nearest scientific notation point and try for the closest"
  51. Print "possible match."
  52. Print "And even though "; Blue&&; " is only one number off from "; _RGB32(0, 0, 255); ","
  53. Print "that number still greatly changes the color value."
  54. Print "It changes it from FF 00 00 FF (255 alpha, 0 red, 0 green, 255 blue) to"
  55. Print "FF 00 01 00 (255 alpha, 0 red, 1 green, 0 blue)."
  56. Print "Our BLUE has become a GREEN, simply by using a SINGLE variable type!!"
  57. Print "(And, it's such a low shade green, my poor eyes can't make it out at all."
  58. Print "To me, the damn 'green box' was just as black as my black screen."
  59. Print "I didn't see it at all!)"
  60. Print "Press <ANY KEY> to continue!"
  61. Cls , 0
  62. Print "So, at this point, I think it should be obvious WHY we don't want to store"
  63. Print "color values inside SINGLE variables."
  64. Print "But what about using a normal LONG to hold the values??"
  65. Print "Let's look and see!"
  66. Print "For this, let's draw our box again:"
  67. Line (50, 150)-(250, 250), _RGB32(0, 0, 255), BF
  68. Locate 18, 1: Print "Looks like a nice BLUE box.  Right?"
  69. Print "But let's get the POINT value from that box, and store it in a LONG variable."
  70. BLUE& = Point(100, 200)
  71. Print "BLUE& = "; BLUE&
  72. p&& = Point(100, 200)
  73. Print "POINT(100, 200) = "; Point(100, 200)
  74. Print "Again, we're looking at two numbers that don't match!"
  75. Print "FOR THE LOVE OF GOD, WHYYYY??!!!!"
  76. Print "Press <ANY KEY> to continue!"
  77. Cls , 0
  78. Print BLUE&; "<>"; p&&
  79. Print "Why are those two numbers so different??"
  80. Print "For that answer, let's look at their HEX values again:"
  81. Print "BLUE& = "; Hex$(BLUE&)
  82. Print "POINT(100, 200) = "; Hex$(p&&)
  83. Print "."
  84. Print "..."
  85. Print "......"
  86. Print "WHAT THE HEX??  Those two values are EXACTLY the same??"
  87. Print "They are.  It's just that one of them is stored as a SIGNED LONG, while the     other is an UNSIGNED LONG."
  88. Print "HEX wise, they're the same value..."
  89. Print "BUT, can you see where the two numbers might not match if we use them in an IF  statement?"
  90. Print "IF "; BLUE&; "="; p&&; "THEN...."
  91. Print "Ummm...  That might not work as intended!"
  92. Print "Press <ANY KEY> to continue!"
  93. Cls , 0
  94. Print "Even thought the HEX values for "; BLUE&; "and"; p&&;
  95. Print "are EXACTLY the same, the values themselves are quite different."
  96. Print "A LONG will, indeed, hold the proper value for a 32-bit color, as it stores"
  97. Print "all four HEX values properly for us."
  98. Print "As long as our program uses NOTHING but LONG values, you'll never have a"
  99. Print "problem with using LONG as a variable type..."
  100. Print "BUT...."
  101. Print "The moment you start to compare LONG values directly against POINT values,"
  102. Print "your program is going to run into serious issues!"
  103. Print "Because at the end of the day,"; BLUE&; "is not the same as "; p&&
  104. Print "Press <ANY KEY> to continue!"
  105. Cls , 0
  106. Print "So, with all those examples, and all that said, let's answer"
  107. Print "the most important question:"
  108. Print "'What TYPE works best for 32-bit colors??"
  109. Print "DOUBLE, _FLOAT, _UNSIGNED LONG, _INTEGER64, _UNSIGNED _INTEGER64"
  110. Print "Of all the types which QB64 offers, only the above are TRULY viable"
  111. Print "to hold a 32-bit color value."
  112. Print "Any type not listed above is going to be problematic at one time or"
  113. Print "another for us!"
  114. Print "And of those suitable types, I personally prefer to keep integer values"
  115. Print "as integers, so I recommend: _UNSIGNED LONG or _INTEGER64."
  116. Print "Press <ANY KEY> to continue!"
  117. Cls , 0
  118. Print "And WHY _UNSIGNED LONG??"
  119. Print "Simply because it's only 4 bytes of memory (the minimal we can possibly use for"
  120. Print "32-bit color values), and it's what QB64 uses internally with POINT and such."
  121. Print "So, if _UNSIGNED LONG works so well, WHY would I *ever* use _INTEGER64??"
  122. Print "Becauses sometimes I like to code command values into my colors."
  123. Print "(Such as: NoColor = -1)"
  124. Print "_UNSIGNED LONG *only* holds the values for the colors themselves."
  125. Print "EVERY number from 0 to FFFFFFFF is accounted for as part of our color spectrum."
  126. Print "If I need *special* or unique values for my program, I usually just use _INTEGER64s"
  127. Print "for my variable types and then I can assign negative numbers for those unique values."
  128. Print "Press <ANY KEY> to continue!"
  129. Cls , 0
  130. Print "At the end of the day though, when all is said and done, you're still the"
  131. Print "one responsible for your own code!"
  132. Print "Use whichever type works for you, and works best for your needs."
  133. Print "Just keep in mind:  Various TYPEs come with various limitations on your code."
  134. Print "_BYTE, INTEGER, (both signed and unsigned) are insane to use..."
  135. Print "SINLGE loses precision.  Expect to lose whole shades of blue...."
  136. Print "LONG may cause issues with POINT, if compared directly...."
  137. Print "_UNSIGNED LONG works fine, any ONLY stores 32-bit color values...."
  138. Print "_INTEGER64 works fine, and can store extra values if necessary...."
  139. Print "DOUBLE and _FLOAT both work, but are floating point values...."
  140. Print "And with all that said and summed up, it's now up to YOU guys to decide what"
  141. Print "works best in your own programs."
  142. Print "As I said, I personally recommend _UNSIGNED LONG or _INTEGER64 in special cases."
  143. Print "But the choice, and the debugging, is entirely up to YOU.   :D"
  144.  

Copy.  Paste.  Compile.  I'll let the code speak for itself and hopefully it'll help folks learn a bit more about WHY certain variables are more suitable for use with 32-bit color values than others.  ;)








« Last Edit: December 14, 2021, 01:31:27 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Steve's Quick Lesson on Number TYPEs and Colors
« Reply #1 on: December 14, 2021, 01:06:31 am »
You musta got that there Virginian schooling Steve...

"And even though 4278190336 is only one number off from 4278190112"

Is that there the new fangled 'common core' stuff?🤓   One number off huh?



or maybe you had a _RGB32(0,0,32) instead of _RGB32(0,0,255) on line 75?
Granted after becoming radioactive I only have a half-life!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Steve's Quick Lesson on Number TYPEs and Colors
« Reply #2 on: December 14, 2021, 01:35:57 am »
I have to include small little typos and such like that, just so see if people are actually paying attention to what I'm saying.  ;D

It's all fixed now.  Sometimes my fingers work faster than my brain.  I was thinking "32-bit colors", so my 255 BLUE ended up turning into a 32 there.   

Maybe I should add a chapter on, "AND MAKE CERTAIN TO USE THE RIGHT VALUES FOR YOUR COLORS".   Only problem with that is, I doubt if I'd ever read it.  I'm a stubborn cuss and have to learn things the hard way by goofing up and glitching them myself first!  Tutorials and lessons be danged!  :P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Steve's Quick Lesson on Number TYPEs and Colors
« Reply #3 on: December 14, 2021, 06:46:12 am »
I do like when someone helps me to think out of the box. I was aware of _UNSIGNED LONG as the "correct" type for 32 bit color, but storing color values in _INTEGER64 with additional and/or command data never occurred to me as a thing that one could do. Thanks for that.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Steve's Quick Lesson on Number TYPEs and Colors
« Reply #4 on: December 14, 2021, 07:04:45 am »
I do like when someone helps me to think out of the box. I was aware of _UNSIGNED LONG as the "correct" type for 32 bit color, but storing color values in _INTEGER64 with additional and/or command data never occurred to me as a thing that one could do. Thanks for that.

I actually use INT64 quite a bit for a color variable in different routines.  Anything positive is usually a color for the code, whereas negative values have preset command uses.  For example, a color value of -1 might mean (No need to redraw), which allows me to completely skip processing cycles on drawing/coloring something on the screen that hasn't changed.  A color value of -2 might mean (Use Default color scheme), which is a normal black on white, or black on light gray color set.  A color value of -2 might mean (Use High Contrast color scheme), which is what many folks laugh at when they see some of my demos on the forums here.  My daughter isn't 100% color blind, but she has colors which fade and blend in together for her.  For instance, she can't tell the difference between a Sea Green and a Sky Blue -- though she can tell a bright green from a deep blue!  Learning to code for HER eyes has led me to create some *very* distinctive color styles over the years, with a bight red on yellow being one of the most common -- which nobody else seems to enjoy using!  LOL!   Having a quick value to swap over to her high-contrast colors has came in handy more than once for me, so I'll often code it in as negative value colors.  ;)

All little tricks which I use to help me with my programs/colors from time to time.

Another place where I'll use negative colors is when I'll make a grid for a map.  Negative values might mean (Map not uncovered yet), while positive values means (Show this part of the map).  It's a very simple little toggle to keep track of where the player has traveled and uncovered, and allows me to create a very simple "fog of war" which greatly encourages map exploration.

_Unsigned Long is my go to for when I just need plain color values.  BLUE~& = _RGB32(0, 0, 255), for example.  Integer64, however, is what I'll often use when I need my color values PLUS just a little bit more.

As I said in the lesson above, it's all up to what the programmer and their program itself needs, to work as bug-free and as efficiently as possible.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!