Hi guys! I'm writing a map editor,
and now i'm stuck on how to convert
RGB to nes palette color.
thanks in advance!
Why do you need to perform this conversion? Are you taking graphics using arbitrary palettes? Are the palettes expected to resemble the actual NES colors? You can try to do the conversion based on what you think the NES palette looks like, but when users have the freedom to use whatever palettes they want you have to ask them to provide the palettes they used so you can convert the graphics properly.
If you understand Python, there is a program that does this in the
source code of the Action 53 menu. It converts 64x56 pixel screenshots to NES tiles, guessing a 4-color palette for each.
It's a million times better to design the graphics around the palette, instead of doing a lossy RGB->Palette conversion.
If you have to though, you can grab an NES palette, and convert to it by taking the input color and finding the closest match in the NES palette (i.e., which color has the closest R, G, and B values). Keep in mind that most emulators use their own palette, and most people use their own palette, so in reality, this is a huge crapshoot.
Convert it from RGB to YUV. Convert UV to phase and amplitude. Quantize the colors to 4 luma levels, 12 phases and 2 amplitudes.
Or just compare to all colors in the palette using a metric like ((r1-r2)^2*3 + (g1-g2)^2*6 + (b1-b2)^2). You're going to have to do this comparing for each pixel even if the user provides an 8- or 32-digit list of palette indices.
Yes, there's no way around the color detection, but the accuracy of this detection might be poor if the program and the artist use very different palettes. I think you should have a default palette, but allow users to provide their own (kinda like emulators do) if they are no satisfied with the default conversion.
That or add a feature to the conversion tool to export the 64-color palette it uses as a PNG, so that the artist can tune the graphic to the palette. That's the approach I'm using.
That could be an option as well, but people are stubborn. There will always be someone that will not let go of their own palette.
tokumaru wrote:
That could be an option as well, but people are stubborn. There will always be someone that will not let go of their own palette.
Me for example.
That's why I said it's better to design the graphics around the palette. It'll look subtly different from person to person, but at least red will be red and blue will be blue.
You know, this is starting to sound an awful lot like the lament of television graphic designers in the 80s and 90s.
Allowing custom palettes has this advantage that editor won't be tied into nes. It could be used for GB/MasterSystem/Other 8bit systems, heck it could be used as general map editor if someone makes game with retro graphics.