• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Simple Bitmap Color Swapper

Boonzeet

Pokémon Secrets of the Ages Developer
188
Posts
15
Years
  • l8GEOrZ.gif


    Simple C++ Library to swap colours of an input RPG Maker Bitmap.

    Takes two input CSV-formatted color palettes to swap, one for the other.

    This allows you to create simple greyscale or other images which can then be dynamically colorized, for example to create a character or item customisation option.

    To use:
    Download Release/PaletteSwap.dll from the GitHub repository. Place this in the root folder of your project.

    Next, place this script above Main:

    Code:
    module PaletteSwap
      VERSION = 1.0
      DLL_NAME = 'PaletteSwap' # Optionally change this to store the DLL elsewhere
    
      module Error
        def self.raise_error(err, obj, msg)
          klass = obj.class.to_s
          stack = caller.slice(1..15)
          stack.each do |i|
            script = i.scan(/\d+/)[0]
            i.sub!(/{\d+}/, $RGSS_SCRIPTS.at(script.to_i)[1]) if script
          end
          raise err, msg % klass << "\n\n#{stack.join("\n")}"
        end
        def self.raise_bitmap(bitmap)
          raise_error(RGSSError, bitmap, "%s is a disposed or invalid bitmap!")
        end
      end
    end
    
    #==============================================================================
    # ** Bitmap
    #==============================================================================
    
    class Bitmap
      @@colorize = Win32API.new(PaletteSwap::DLL_NAME, 'Colorize', ['l', 'p', 'p'], 'l').freeze
    
      # Colorize call
      def colorize!(og_palette, new_palette)
        PaletteSwap::Error::raise_bitmap(self) if self.disposed?
        @@colorize.call(object_id, og_palette, new_palette)
      end
    end

    Then call like so (example from Essentials):

    Code:
    bmp = BitmapCache.load_bitmap("example.png")
    bmp.colorize!("75,75,75\n105,105,105\n135,135,135", "50,65,98\n65,96,159\n57,119,198")

    The palette sizes should be the same length. Please ensure these strings are properly formatted with the RGB values separated by commas and each separate color separated by "\n".

    This will swap each color in the first list, e.g. rgb(105,105,105), with the corresponding item in the second list, i.e. rgb(65,96,159).

    I'm using this for a clothing generator, but I thought this could potentially have more uses to a wider audience.

    Credit to Copern for the error handling in the RGSS script, which is based on his version.
     
    Last edited:
    24
    Posts
    5
    Years
    • Seen Jul 24, 2023
    Hello, and thank you for sharing your resource!

    By my understanding, this allows you to edit the displayed colors that essentials uses when showing an image. So you could change all of the red pixels on trback000.png to blue whenever it is displayed, or something like that. Is that correct?

    Also, when you say to call it - does that mean you'd call it from within an event in RMXP (using the Script command), or you'd call it from within a script that you'd need to write?

    Again, thanks for sharing.

    Edit: I've been tinkering around trying to figure this one out. I have the .dll in the root folder. I've added the script above main, and am calling it from within an event using the Script command. I've checked and double checked that my filepath to the .png is correct, and that the string that contains the RGB color combos is correctly formatted, and am using the RGB numbers from Photoshop when I select the color. When I interact with the event, I don't get any errors, but also none of the colors change on the event's graphics. I figure there must be something simple that I'm missing here!
     
    Last edited:

    Boonzeet

    Pokémon Secrets of the Ages Developer
    188
    Posts
    15
    Years
  • Hello, and thank you for sharing your resource!

    By my understanding, this allows you to edit the displayed colors that essentials uses when showing an image. So you could change all of the red pixels on trback000.png to blue whenever it is displayed, or something like that. Is that correct?

    ...

    Yes, if you input red and blue on the respective palettes all red pixels would be colored blue.

    This library only works with a passed-in Bitmap, and it doesn't store or save it anywhere. You'll need to write a command to save it to file, or explore if event graphics can be set to Bitmaps (I've not tried this).

    Code:
    filepath = "file-example.png"
    bmp = BitmapCache.load_bitmap(filepath)
    bmp.colorize!("255,0,0", "0,0,255")
    bmp.saveToPng("Graphics/Characters/example.png")

    Here's a bit of mock code how that could work
     
    Back
    Top