The following is in reference to
this thread where i'm attempting to offer a method to determine the proper prefecture on a map based on the pointer's position using what i'm calling a ColorMap vs the theoretically more-demanding system using hundreds of sprite shapes to create a sort of
Image Map for lookup found there (if there's a proper term for this ColorMap, let me know?):
// Project: ColorMap
// Created: 2020-08-20
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "ColorMap" )
SetWindowSize( 800,600, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
SetWindowPosition(200,200)
// set display properties
SetVirtualResolution( 800,600 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
GLOBAL ColorMap as Integer : ColorMap = LoadImage("colormap.png")
GLOBAL MemMap as Integer : MemMap = CreateMemblockFromImage( ColorMap )
GLOBAL Width as Integer : Width = GetMemblockInt(MemMap, 0)
GLOBAL Height as Integer : Height = GetMemblockInt(MemMap, 4)
GLOBAL Size as Integer : Size = GetMemblockSize(MemMap)
GLOBAL XOff as Integer = 200 : GLOBAL YOff as Integer = 100
GLOBAL Map as Integer : Map = CreateSprite(ColorMap) : SetSpritePosition(Map,XOff, YOff)
Type PrefData
Color as Integer
Label as String
EndType
GLOBAL Prefectures as PrefData []
AddPrefecture(-16711681, "Aomori")
AddPrefecture(-16715536, "Akita")
AddPrefecture(-16719391, "Iwate")
AddPrefecture(-16723246, "Yamagata")
AddPrefecture(-16727101, "Miyagi")
AddPrefecture(-16730956, "Fukushima")
AddPrefecture(-256, "Nigata")
AddPrefecture(-987136, "Ishikawa")
AddPrefecture(-1974016, "Toyama")
AddPrefecture(-2960896, "Nagano")
AddPrefecture(-5921536, "Yamanashi")
AddPrefecture(-3947776, "Fukui")
AddPrefecture(-4934656, "Gifu")
AddPrefecture(-6908416, "Aichi")
AddPrefecture(-7895296, "Shizuoka")
AddPrefecture(-16711936, "Gunma")
AddPrefecture(-16715776, "Tochigi")
AddPrefecture(-16723456, "Saitama")
AddPrefecture(-16731136, "Tokyo")
AddPrefecture(-16719616, "Ibaraki")
AddPrefecture(-16734976, "Kanagawa")
AddPrefecture(-16727296, "Chiba")
do
x = GetPointerX() : y = GetPointerY()
If GetSpriteHit(x,y) = Map
ThisColor = PickColor(x,y)
If GetPointerPressed() then SetClipboardText(STR(ThisColor))
Endif
Print (STR(X-XOff) + "x" + STR(Y-Yoff))
Print (ThisColor)
Print (GetPrefecture(ThisColor))
If GetSpriteHit(x,y) = Map then Print (GetPre2(x,y))
Sync()
loop
function GetPrefecture(Color)
ThisPrefecture$ = "None Found"
index = Prefectures.find(Color)
If Index > -1 then ThisPrefecture$ = Prefectures[Index].Label
EndFunction ThisPrefecture$
Function GetPre2(x,y)
x = x - XOff : y = y-Yoff
offset = 12 + (((Width * y) + x) * 4)
R = GetMemblockByte(MemMap, offset )
G = GetMemblockByte(MemMap, offset + 1)
B = GetMemblockByte(MemMap, offset + 2)
ThisColor = MakeColor(R,G,B)
ThisPre2$ = "None Found"
Index = Prefectures.find(ThisColor)
If Index > -1 then ThisPre2$ = Prefectures[Index].Label
EndFunction ThisPre2$
Function PickColor(X,Y)
clearScreen()
setScissor(X,Y,X+1,Y+1)
render()
img = getImage(X,Y,X+1,Y+1)
mem = createMemblockfromImage(img)
rem get memblock data
r = getMemblockbyte(mem,12):rem gets red channel data
g = getMemblockbyte(mem,13):rem gets green channel data
b = getMemblockbyte(mem,14):rem gets blue channel data
//a = getMemblockbyte(mem,15):rem gets alpha channel data
rem tidy up
deletememblock(mem)
deleteimage(img)
setScissor(0,0,getDeviceWidth(),getDeviceHeight())
clearScreen()
color = makeColor(r,g,b)
//color=0
endfunction color
Function AddPrefecture(Color,Label$)
ThisPref as PrefData
ThisPref.Color = Color
ThisPref.Label = Label$
Prefectures.Insert(ThisPref)
Prefectures.Sort()
EndFunction
There are 2 separate things demonstrated where the second relies upon the first using the attached image (based on
this original) where i simply took the image into photoshop and assigned each prefecture a unique color/shade based on region.
once loaded and displayed, i used one of the PickColor functions found on the forums (
specifically, this one by fubarpk) to pull colors and added them (and the prefecture name) manually into an array for lookup.
with that, we can already hover over a given color to determine prefecture, tho it's not an ideal method because 1) you'll lose proper colors along the borders if maximized/resized, 2) copious use of GetImage() = performance hit/bad practice, et al.
instead, the goal was to use an image memblock to retrieve the color (GetPre2() Function). it works, but there is an error when you dip below the sprite's boundary (sometimes, and not obvious to me. any help there is appreciated, please
).
i've been wanting to explore this idea for one of my own (future) projects and thought i'd put something together and share it here. feel free to critique and offer any advice!