Main Menu

News:

SyntaxBoom, now with pwetty syntax highlighted code boxes! \o/ 

https://www.syntaxboom.com/forum/index.php?topic=96

Shoutbox

Dabzy

Today at 11:30:13
Anyway, she found the prosecco and choccies in the bucket, and was over the moon with the setup, and I got a big hug for my effort! Hehehe

Dabzy

Today at 11:28:43
Well, she loved it, when I her the mop and bucket she was like "Dabz, what the hell?", and I said "I'm thinking ahead Glenda, see, your getting on a bit, and if you inadvertanly do a wee on the kitchen floor... Voila, your sorted" :D

GfK

2025-10-18, 17:45:33
My mop and bucket was £30. 🤨 It's a Vileda one and you stick it in the bucket and press a pedal with your foot and it spins round and dries the mop out AND IT'S BRILLIANT! 👌

Dabzy

2025-10-18, 16:19:35
Oh, I can give you a tip right now, no need to wait... Ahem... Pay someone a fiver to do it, even the bucket was a chew! :D ;)

Jackdaw

2025-10-18, 16:04:05
Well if the occasion should ever arise, where I need to gift wrap a mop and bucket. I know where to get tips from ;)

Dabzy

2025-10-18, 14:24:38
Has anyone ever tried to wrap up a mop? Never again! :D

Dabzy

2025-10-18, 12:17:43
Lol, not the ones from Poundland! :D Anyway, I've got a nice box of chocolates and a Prosecco to go in the bucket! ;)

Jackdaw

2025-10-18, 11:18:06
@Dabzy I think they will appreciate it. Have you seen how much it can cost for a good mop and bucket set in this day and age.  I can buy a bottle of Blonde chocolate cream liqueur for less at ASDA.

Dabzy

2025-10-18, 10:47:41
Well, I'm off out to buy a 60th birthday pressie... A mam of one of me mates... Gonna get her a mop and bucket, and wrap it up, even wrap the noodly mop bits individually... All in Christmas wrapping paper... Should go down well! :D

Dabzy

2025-10-17, 17:36:22
Well, just have to keep an eyes, even though they said they sorted it like

Members
Stats
  • Total Posts: 1,828
  • Total Topics: 226
  • Online today: 20
  • Online ever: 232 (Oct 08, 2025, 09:18 AM)
Users Online
  • Users: 0
  • Guests: 13
  • Total: 13
Welcome to SyntaxBoom. Please login or sign up.

Recent

Int To Object

Started by _PJ_, Aug 09, 2025, 08:47 PM

Previous topic - Next topic

_PJ_

(Technically this is NG issue, but I am assuming it is the same in "vanilla" BMax)

So I was wanting to automate the process of building a palette library for a bunch of sprite images (so I can later simplify the images and also do some code-based palette swapping etc)

but I ran into some issues with trying to make a list of the pixel values, as the TList commands refer to Object arguments.

Usually, Strings and whatever can simply recast as "Object" but not Integers it seems:::

Here's a simplified example:
Global Palette:TList

Local Image:TPixmap=LoadPixmap("Test.png")

For Local Y:Int= 0 Until Image.height
	For Local X:Int= 0 Until Image.width
		Local Pixel:Int=ReadPixel(Image,X,Y)
		If ( Not (ExistInPalette(Pixel))) Then AddToPalette(Pixel)
	Next
Next

DebugLog("Found "+Palette.Count()+" colours")


Function ExistInPalette:Byte(Pixel:Int)
	If (Palette=Null) Then Return False
	Return Palette.Contains(Pixel)
End Function

Function AddToPalette(Pixel:Int)
	If (Palette=Null) Then Palette=New TList
	Palette.AddLast(Pixel)
End Function

Working with an array may be easier, iterating through each array item, but I don't know how big the array may need to be in advance...

Sledge

You might be better off with an IntMap rather than a List:

https://blitzmax.org/docs/en/api/brl/brl.map/tintmap/

You could use the Int pixel values as keys (the actual value the key returns wouldn't really matter -- a string representation of the key would be my choice). You can check for a key before adding via Contains(), then at the end of the process get a collection of all the keys with Keys(), which would be your entire palette.

It's a bit of a weird case for a Map because the keys themselves are the values that you care about, but hey ho!

Midimaster

Local Image:TPixmap=LoadPixmap("Test.png")

For Local Y:Int= 0 Until Image.height
	For Local X:Int= 0 Until Image.width
		Local Pixel:Int=ReadPixel(Image,X,Y)
		TPalette.Add Pixel
	Next
Next

Type TPalette
	Global List:TList= New TList
	Field Pixel:Int

	Function Add(Pixel:Int)
		If Contains(Pixel)=True Then Return
		Item:TPalette = New TPalette
		Item.Pixel = Pixel
		List.AddLast Item
	End Function 

	Function Contains:Int(Pixel)
		For Local Item:TPalette = EachIn List
			If Item.Pixel=Pixel then Return True
		Next 
		Return False
	End Function 
End Type

_PJ_

Awesome!
Thanks so much for the qwuick replies :)

@Sledge I am not very familiar with the "Map" types and so was a little intimidated to try and risk overcomplicating and making things a lot worse - but thanks for the explanation - it would certainly be a neater approach!


@Midimaster - Thanks. I guess having the Pixel explicit cast as integer within the type scope and then using a custom iteration "contains" helps the compiler to interpret the Object?

What's funny as I originally wrote a function with similar iteration and equivalence check, but then changed it when I remembered the "contains" method :D

Midimaster

Quote...using a custom iteration "contains" helps the compiler to interpret the Object?...

No it is the Local x:Type EachIn, that picks only the searched type in a list.

If your list contains objects of different types then the code line...

QuoteFor Local Item:TPalette = EachIn List

... already casts and limits the iteration to only one type.

_PJ_

Got it!

Thanks, Midimaster