Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
LOG helps find a power
#1
LOG came in handy for me recently when I was reducing LOC (Lines Of Code) for Classic 2048 Game from Dav's version.

He had all these lines to create a shaded Backcolor for a tile, to get darker as value gets higher:
Code: (Select All)
Select Case board(x, y)
    Case 2: bg& = _RGB(239, 229, 218)
    Case 4: bg& = _RGB(236, 224, 198)
    Case 8: bg& = _RGB(241, 177, 121)
    Case 16: bg& = _RGB(236, 141, 84)
    Case 32: bg& = _RGB(247, 124, 95)
    Case 64: bg& = _RGB(233, 89, 55)
    Case 128: bg& = _RGB(242, 217, 107)
    Case 256: bg& = _RGB(238, 205, 96)
    Case 512: bg& = _RGB(238, 205, 96)
    Case 1024: bg& = _RGB(238, 205, 96)
    Case 2048: bg& = _RGB(238, 205, 96)
    Case 4096: bg& = _RGB(121, 184, 226)
    Case 8192: bg& = _RGB(121, 184, 226)
    Case 16384: bg& = _RGB(121, 184, 226)
    Case 32768: bg& = _RGB(60, 64, 64)
    Case Else: bg& = _RGB(204, 192, 180)
End Select

I said to myself if I didn't mind going color blind a bit, I could shade tiles darker by finding the power of 2 that the Board(x, y) value and do bg& in shades of grey: _RGB32(OneValue0to255) = shade of grey
Code: (Select All)
If Board(x, y) Then power = Log(Board(x, y)) / Log(2) Else power = 0
bg& = _RGB32(255 - 17 * power) ' =set to shade of grey, the higher the value the darker

Mission accomplished!

Here is a little formula for getting Power in X of Y. In example above, what power of 2 is Board(x, y) value?
PowerX = Log(Y)/Log(X)

Check if Y is 0 first though to avoid error.
b = b + ...
Reply
#2
Thanks for sharing @bplus this is great stuff.

https://mathinsight.org/logarithm_basics helped explain more about LOG

Going to re-review your code after grokking all of this.

Also, the 17 * power - this is shade of gray how? _RGB32 with one argument makes a gray? or I guess uses the 1 arg for all places in the R G B ?
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#3
Yes, one argument for _RGB32(X) makes a shade of gray, just like _RGB32(X, X, X) would be.
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)