About Monkey 2 › Forums › Monkey 2 Projects › code for loading and saving ted21 color palettes
This topic contains 7 replies, has 2 voices, and was last updated by 
 AdamStrange
 2 years, 7 months ago.
- 
		AuthorPosts
 - 
		
			
				
August 27, 2016 at 8:01 am #3497
With ted21 I am trying to be as open as possible.
Here is the first code to load and save color palettes
a color palette is a block of colors (up 256) with additions 8×8 grid information
The file extension is .mx2palette
layout of the saved file is:
String:”mx2palette”
UInt:count
Uint:gridx
Uint:gridy
colors. these are saved as
float:red
float:green
float:blue
float:alpha
and here is the loading code
Monkey12345678910111213141516171819202122232425262728293031323334353637method LoadPalette( path:string )Local file := Stream.Open( path, "r" )If Not file Then ReturnLocal header:string = file.ReadCString()If header = "mx2palette" ThenLocal count:int = file.ReadUInt()If count < 256 ThenLocal gridX:int = file.ReadUInt()If gridX < 8 ThenLocal gridY:int = file.ReadUInt()If gridY < 8 ThenLocal k:intLocal red:floatLocal green:floatLocal blue:floatLocal alpha:float_gridCount = countGridX = gridXGridY = gridYFor k = 0 Until countred = file.ReadFloat()green = file.ReadFloat()blue = file.ReadFloat()alpha = file.ReadFloat()_color[ k ] = New Color( red, green, blue, alpha )NextEnd IfEnd IfEnd IfEnd Iffile.Close()RequestRender()End methodand here is the saving code
Monkey12345678910111213141516171819202122method SavePalette( path:string )Local file := Stream.Open( path, "w" )If Not file Then ReturnLocal header:string = "mx2palette"file.WriteCString( header )file.WriteUInt( _gridCount )file.WriteUInt( _gridX )file.WriteUInt( _gridY )Local k:intLocal col:uintFor k = 0 until _gridCountfile.WriteFloat( _color[ k ].R )file.WriteFloat( _color[ k ].G )file.WriteFloat( _color[ k ].B )file.WriteFloat( _color[ k ].A )Nextfile.Close()End methodAugust 27, 2016 at 11:14 am #3501Ok, some (stupid maybe!) questions :D!
1. what is for the ‘grid’?
2. why choose a proprietary (even if public) format and not use the .json system – already built in MX2? Considering the possibility to open them directly in TED2?August 27, 2016 at 11:40 am #3502simple. speed
grid is the uxcolorgrid control
as to not using .json:
text is a very poor storage format for anything other than text. you need to parse it and it also needs carful construction.
It is also editable. which means unknown errors creep in
finally. There is no reason for the color table to be readable by humans. its a color! in the same way as a sound is stored in the best format for it, etc
They can be opened in ted2. but Mark will have to provide that.
let say the read file has a grid of 7,3
the shown grid will be 7 cell wide, and 3 cells high with a total of 21 color cells
It would look like this:
Attachments:
August 27, 2016 at 12:14 pm #3504Ok, understood what is the ‘grid’
It would better if the ‘grid layout’ will be managed automatically, and not based on what is ‘stored’… I mean, I need the colors, not the way they are shown.
Yes, I admit json is quite a bit ‘redundant’ for low information.
for a plain text I would use ToARGB and FromARGB method to store colors… it should be faster (in reading/writing I mean).August 27, 2016 at 1:07 pm #3505Here’s a thing. I did initially write the input/output with ToARGB and FromARGB.
But… for some really odd reason ToARGB never gave the correct output, so I went with just writing the direct values instead.
the gridx and gridy values can be completely ignored. They are there for display only and to prevent problems with counts that could be multiples. E.G. a count of 36. is that 6×6 or 4X8 or 3×12, etc
similarly a count of 12 is that 3×4 or 4×3.
So to just read the colors,
read the header, the count and then ignore the next two read in numbers and then just read the colors.
Here’s a shot of the new palette grid view in operation with Ted21. You can see the color panel is also open. pressing “use color” will change the current selected color in the palette and make the document “dirty” so it is automatically saved
Attachments:
August 27, 2016 at 2:15 pm #3507But… for some really odd reason ToARGB never gave the correct output, so I went with just writing the direct values instead.
What does this means? There’s an error in the conversion?
Anyway, as user, I would like to ‘save’ my preferred color (maybe the first 8/16 etc).
August 28, 2016 at 6:05 am #3515i think i used something like colout:uint = colin.ToARGB and it never set.
The other thing was when I looked at the code I expected it to be ABGR and it wasn’t it was the reversed ARGB. which looks fine on paper, but is actually reversed in code.
OK Saving and using color palettes of different sizes – no problem. Here’s a few different ones. I tried to be as flexible as I could be. It also shows where the grid properties are used.
Attachments:
August 28, 2016 at 6:11 am #3520Here’s the code from std/color.monkey2
Monkey123456#rem monkeydoc Converts the color to 32 bit ARGB format.#endMethod ToARGB:UInt()Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)EndGreat. But it really should be this:
Monkey1Return UInt(a*255) Shl 24 | UInt(b*255) Shl 16 | UInt(g*255) Shl 8 | UInt(r*255)where the first byte is the red byte, the second green, third blue and finally alpha
This makes it really ABGR. but you read from right to left so it is actually RGBA which is the proper representation.
If I recall correctly, Microsoft had to rewrite an entire windows codebase because of this (long before xp).
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.





