Bitmap Remapping (Scripting Tool)

Bitmap Remapping
v1.2

Introduction


As I told Zeriab earlier today, I had the idea for this script long ago, when I was modding Command & Conquer - Operation Tiberian Sun. That game used specific shades of red as remapable colors to use with the respective player colors to make units and buildings look individual for each player. This script isn't an exact translation in any way really, but yeah.. that's where I got the idea from.
The purpose of this script is simple... imagine you have a range of colors of the same make (aka dark red, with shading variations) and you'd rather have dark blue, but as you know as a spriter, changing the hue doesn't always do the trick, and as you know as a scripter, doing things with scripts is way cooler anyway. Oh, and last but not least, as you know as a game designer: Having only one sprite for nearly unlimited variations without additional save space needed is great stuff.


Features


  • Pre-defined palettes for instant use with Photoshop-swatches sprited resources
  • Exchange whatever range of colors with any other within the default RGB(A) range
  • Adjust the script to your spriting needs by adding own palettes with ease
  • Modify Alpha values for individual pixels by array input



Script


  1. #==============================================================================
  2. # Bitmap Remapping                                                                                     v1.2
  3. #------------------------------------------------------------------------------
  4. # Script by BlueScope
  5. #==============================================================================
  6.  
  7. class Bitmap
  8.   #--------------------------------------------------------------------------
  9.   def remap(remap_color, new_color, remap_alpha=255, new_alpha=255)
  10.      mapping = colorset(remap_color, remap_alpha)
  11.      replace = colorset(new_color, new_alpha)
  12.      if mapping.size <= replace.size
  13.         for x in 0..self.width
  14.            for y in 0..self.height
  15.               for i in 0...mapping.size
  16.                  if self.get_pixel(x, y) == mapping[i]
  17.                     self.set_pixel(x, y, replace[i])
  18.                  end
  19.               end
  20.            end
  21.         end
  22.      end
  23.   end
  24.   #--------------------------------------------------------------------------
  25.   def colorset(id, alpha)
  26.      if alpha.is_a?(Integer) or alpha.size < 5
  27.         alpha = [alpha, alpha, alpha, alpha, alpha]
  28.      end
  29.      case id
  30.      when 0 # Skin Color
  31.         color = [Color.new(254, 254, 254, alpha[0]),
  32.                       Color.new(255, 217, 173, alpha[1]),
  33.                       Color.new(235, 179, 133, alpha[2]),
  34.                       Color.new(212, 140, 82, alpha[3]),
  35.                       Color.new(117, 62, 45, alpha[4])]
  36.      when 1
  37.         color = [Color.new(242, 154, 121, alpha[0]),
  38.                       Color.new(235, 102, 69, alpha[1]),
  39.                       Color.new(227, 0, 26, alpha[2]),
  40.                       Color.new(157, 13, 21, alpha[3]),
  41.                       Color.new(121, 14, 17, alpha[4])]
  42.      when 2
  43.         color = [Color.new(247, 179, 131, alpha[0]),
  44.                       Color.new(241, 143, 78, alpha[1]),
  45.                       Color.new(233, 93, 15, alpha[2]),
  46.                       Color.new(162, 67, 17, alpha[3]),
  47.                       Color.new(124, 51, 14, alpha[4])]
  48.      when 3
  49.         color = [Color.new(252, 203, 140, alpha[0]),
  50.                       Color.new(248, 179, 86, alpha[1]),
  51.                       Color.new(243, 148, 0, alpha[2]),
  52.                       Color.new(168, 104, 4, alpha[3]),
  53.                       Color.new(128, 80, 7, alpha[4])]
  54.      when 4
  55.         color = [Color.new(255, 246, 155, alpha[0]),
  56.                       Color.new(255, 241, 95, alpha[1]),
  57.                       Color.new(255, 237, 0, alpha[2]),
  58.                       Color.new(180, 165, 0, alpha[3]),
  59.                       Color.new(136, 126, 0, alpha[4])]
  60.      when 5
  61.         color = [Color.new(208, 224, 153, alpha[0]),
  62.                       Color.new(185, 209, 101, alpha[1]),
  63.                       Color.new(151, 191, 13, alpha[2]),
  64.                       Color.new(103, 135, 19, alpha[3]),
  65.                       Color.new(77, 104, 17, alpha[4])]
  66.      when 6
  67.         color = [Color.new(177, 211, 152, alpha[0]),
  68.                       Color.new(136, 191, 103, alpha[1]),
  69.                       Color.new(65, 166, 42, alpha[2]),
  70.                       Color.new(39, 118, 33, alpha[3]),
  71.                       Color.new(24, 91, 26, alpha[4])]
  72.      when 7
  73.         color = [Color.new(144, 199, 151, alpha[0]),
  74.                       Color.new(71, 173, 104, alpha[1]),
  75.                       Color.new(0, 144, 54, alpha[2]),
  76.                       Color.new(0, 103, 41, alpha[3]),
  77.                       Color.new(0, 80, 31, alpha[4])]
  78.      when 8
  79.         color = [Color.new(138, 204, 199, alpha[0]),
  80.                       Color.new(48, 179, 173, alpha[1]),
  81.                       Color.new(0, 151, 144, alpha[2]),
  82.                       Color.new(0, 107, 102, alpha[3]),
  83.                       Color.new(0, 82, 79, alpha[4])]
  84.      when 9
  85.         color = [Color.new(132, 208, 241, alpha[0]),
  86.                       Color.new(0, 185, 233, alpha[1]),
  87.                       Color.new(0, 158, 225, alpha[2]),
  88.                       Color.new(0, 111, 159, alpha[3]),
  89.                       Color.new(0, 84, 122, alpha[4])]
  90.      when 10
  91.         color = [Color.new(139, 174, 216, alpha[0]),
  92.                       Color.new(72, 141, 199, alpha[1]),
  93.                       Color.new(0, 106, 179, alpha[2]),
  94.                       Color.new(0, 74, 127, alpha[3]),
  95.                       Color.new(0, 55, 98, alpha[4])]
  96.      when 11
  97.         color = [Color.new(142, 154, 201, alpha[0]),
  98.                       Color.new(87, 115, 178, alpha[1]),
  99.                       Color.new(0, 74, 153, alpha[2]),
  100.                       Color.new(0, 51, 110, alpha[3]),
  101.                       Color.new(0, 36, 84, alpha[4])]
  102.      when 12
  103.         color = [Color.new(144, 133, 186, alpha[0]),
  104.                       Color.new(97, 86, 157, alpha[1]),
  105.                       Color.new(23, 41, 131, alpha[2]),
  106.                       Color.new(15, 27, 95, alpha[3]),
  107.                       Color.new(13, 15, 73, alpha[4])]
  108.      when 13
  109.         color = [Color.new(171, 140, 188, alpha[0]),
  110.                       Color.new(138, 91, 157, alpha[1]),
  111.                       Color.new(98, 33, 129, alpha[2]),
  112.                       Color.new(70, 21, 93, alpha[3]),
  113.                       Color.new(54, 3, 71, alpha[4])]
  114.      when 14
  115.         color = [Color.new(197, 147, 190, alpha[0]),
  116.                       Color.new(174, 96, 158, alpha[1]),
  117.                       Color.new(147, 17, 126, alpha[2]),
  118.                       Color.new(105, 8, 90, alpha[3]),
  119.                       Color.new(81, 0, 69, alpha[4])]
  120.      when 15
  121.         color = [Color.new(241, 159, 193, alpha[0]),
  122.                       Color.new(234, 105, 160, alpha[1]),
  123.                       Color.new(227, 0, 122, alpha[2]),
  124.                       Color.new(160, 0, 87, alpha[3]),
  125.                       Color.new(124, 0, 66, alpha[4])]
  126.      when 16
  127.         color = [Color.new(242, 157, 160, alpha[0]),
  128.                       Color.new(235, 104, 120, alpha[1]),
  129.                       Color.new(227, 0, 79, alpha[2]),
  130.                       Color.new(159, 0, 56, alpha[3]),
  131.                       Color.new(122, 3, 42, alpha[4])]
  132.      when 17
  133.         color = [Color.new(206, 188, 165, alpha[0]),
  134.                       Color.new(163, 144, 124, alpha[1]),
  135.                       Color.new(125, 105, 90, alpha[2]),
  136.                       Color.new(89, 73, 63, alpha[3]),
  137.                       Color.new(56, 48, 43, alpha[4])]
  138.      when 18
  139.         color = [Color.new(204, 166, 116, alpha[0]),
  140.                       Color.new(175, 133, 84, alpha[1]),
  141.                       Color.new(149, 105, 57, alpha[2]),
  142.                       Color.new(125, 80, 36, alpha[3]),
  143.                       Color.new(103, 59, 21, alpha[4])]
  144.      else
  145.         color = [Color.new(254, 254, 254, alpha[0]),
  146.                       Color.new(192, 192, 192, alpha[1]),
  147.                       Color.new(128, 128, 128, alpha[2]),
  148.                       Color.new(64, 64, 64, alpha[3]),
  149.                       Color.new(1, 1, 1, alpha[4])]
  150.      end
  151.      return color
  152.   end
  153.   #--------------------------------------------------------------------------
  154. end
Expand to see the code.



Instructions


First, you should know about the palettes... I included 20 pre-defined ones, referenced to by an index number. Here are your options:

  • 0 :: RMVX's default skin color
  • 1-18 :: The default Adobe Photoshop swatches palettes (image below)
  • > 18 :: hard grayscale


Image


Of course, you can - and should - add own palettes to the list. You alternatively can modify the sprites you're using to your needs. A combination of both will ultimately make this script powerful to you. If you want to advance the palettes I gave ya, you can do that... you can add as many colors to a palette as you want, as long as you keep their count equal between palettes. You can exchange a larger palette with a smaller one, but not vice versa - in the latter case, no color will be exchanged simply, but you don't get any error message shutting your game down.


It operates by switching out two palettes, so you don't need to do anything else than giving the method two values. Syntax is the following:

  1. @sprite = Sprite.new
  2. @sprite.bitmap = ...
  3. @sprite.bitmap.remap(remap_color, new_color)
Expand to see the code.


So if, for example, your sprite has hair colored with the colors from palette #1, but you want them to be blue like palette #10, you use @sprite.bitmap.remap(1, 10) - as soon as you have your resources and palettes, it's easy as hell.

What you can also do is give specific alpha values. With that, you can check for transparency values in colors, giving you a bit more of adjustability. All you need to do is change the last line given above like this:

  1. @sprite.bitmap.remap(remap_color, new_color, remap_alpha, new_alpha)
Expand to see the code.


Keep in mind that the last two values can be numbers from 0 to 255, as well as arrays with the size of 5. That means instead of a single number, you can use [60, 80, 100, 120, 140], giving the first color the alpha value 60, the second one 80, and so on.
Also, they have 255 as default value, so if you don't input anything, you just get the completely opaque pixels.


FAQ


Q: Will this script work for RMXP and RMVX alike?
A: I didn't test it, but it should run completely fine on either platform.

Q: This script is really slow on big graphics! What can I do?
A: You can try poccil's/BwdYeti's independent color remapping in the [url]Script Snippets[/url] topic (somewhere on page 2) and merge it with my script. That shouldn't be hard to do, but personally, I prefer non-dll scripts, which is why there won't be a version of that in this topic.


Compatibility


Unless any other scripts have the same names as the ones in my script, it's perfectly compatible, as it's a simple add-on - no modified methods.


Credits and Thanks


BlueScope, for translating the concept and doing the scripting and fucked-up palette data work,
Zeriab, for pointing out that I messed up the palettes,
BlueScope, for fixing them ^^"


Terms and Conditions


You may use and/or modify this script in any way you want, and/or release your modifications, as long as you give proper credit to me, BlueScope. You may not use this for commercial projects (in this context refering to profit-oriented) of any sort, including non-RPG Maker projects. You may not remove or modify the original header included in the script.
The actual mentioning of my nickname at some point in the game in a crediting context (i.e. opening credits, ending credits) is not necessary, but apprechiated.
This could be extrmely useful with a character creation system, if I remebmer how those work. You could easily make it where each individual portion of the character's body/clothing could have the color selected, without having to make a sprite for every one. In fact, this would be a very good addition to an online game too, because it makes it easier to give everyone a unique item. I haven't tried it, but, if it works like you say it does, good job!
Zeriab tried it, and he said it "works like a charm", if I might quote him here ^^ Also, that's exactly what I originally planned it to be for, before I figured I might as well make it a scripting tool for all bitmaps... and yeah, you can change as many colors as you want, for example for a charactersheet, so you can have a sprite with simultaneously remapable hair, skincolor, armor, sleeves, belt, legs, shoes, accessories, ... unlimited possibilities here. Oh, and there's tilesets, of course ;)
That would be very cool I must say for my GUI system Indeed, I was thinking in doing somthing like this, as I said to you :P. But well I have so many things in my hands that I decided to keep that for last, works great for what I tried =).

Nice work BlueScope!
This looks very nifty. I think I'm going to take this and run it through RPG::Cache when I build my character creation/visible equipment script.
@mew: Well, while that is the purpose of the script really, I surely don't need to tell ya that this method is highly resource-consuming, potentially rising by increasing dimensions of your bitmap. ^^"
Which is exactly why it needs to be in RPG::Cache, so that it doesn't have to call the method every single time it needs the image.
Who would've thought it, but this script got updated. It's only a small change only a few people will need really, but it's still a cool thing: Alpha transparency access using the remap method - in other words, you can make sprites have different opacity levels. And all of that on the cost of only one more line and a few more words ^^

This was just my lunch break boredom ban, so I might release a v1.2 soon which is on my mind at the moment... I'm thinking about utilizing arrays as alpha values, so you can address each color by it's own. Ah well, lemme see about that. (EDIT: Screw that, I was just napping some minutes from my worktime to get this done... so yeah, v1.2 is up!)

Please tell me if you know anything else to add to this.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=60412