Converting iTerm2 colours to Windows Terminal colors

I want to convert an iTerm2 colour scheme such as this one (Ubuntu) to the Windows Terminal color scheme. I have no idea how to do this! I have no idea what those iTerm2 colour schemes even mean. It is an XML file with what looks like RGB values in decimal. Moreover, instead of specifying colours it has entries like “Ansi 1 Color” etc. Whatever that means!

Here’s an excerpt of the file:

I want to try and figure out what these mean and how I can convert them to an iTerm2 format. Going to try and do this as I make notes in this blog. Hence some of the steps below might seem obvious or elementary. 

I can download a colour scheme thus:

And quickly view the colour keys thus:

Here’s the output:

Since each <key> element has a <dict> too I can view those thus:

Output:

What does this mean? RGB scales are usually on a 0 – 255 scale, so this confused me. After some Googling I realised you can have them on a 0 – 1 scale too. Thanks to StackOverflow. So a number X on the 0 – 255 scale can be converted to the 0 – 1 scale by dividing it by 255. Therefore a number in the 0 – 1 range above can be converted to 0 – 255 scale by multiplying it by 255. From there it’s an easy step to converting to RGB values in hex. Cool!

At this point I have the following rudimentary script:

If I do $valuesArray[0] I get the first line, and if I do $valuesArray[0].real I get an array of 3 colors – Blue, Green, and Red. 

Ok, so I need something that will convert these to hex. Time to create a function:

Let’s try it out:

Cool! Quick test with my array:

Nice! I can just capture this into a new array:

So at this point I have a $hexColorsArray with the hex values of the colors, and I have a $keysArray with the “Ansi 0 Color” etc. whatever that is. 

Here’s a place where one can find Windows Terminal themes. A sample theme looks like this:

I have no idea how to map the iTerm2 keys to these! Eugh. 

Here’s what the colors section of my iTerm2 looks like:

Ok, so that’s 8 colours (Black – White) along with their bright variants. Hmm, those line up with the colours on this Wikipedia page too. And I have similar entries in Windows Terminals, so what I need is a mapping like this:

That’s not all the colours as I am missing the following from the iTerm2 side:

Hmm, turns out the sample colour scheme I was looking at is not complete. Looking at the official docs here’s a default colour scheme:

So I’ve got four more to add. There’s three entries that I don’t have a mapping for so I’ll make a dummy mapping for these now, and prefix with an exclamation mark to ignore them later. Let’s put this into a function:

I can convert from one to another thus:

So at this point I have $winColorNamesArray and $hexColorsArray array. All I have to do now is output a JSON colour scheme. That’s easy, loop through the arrays, ignore the colours I marked earlier, make a hash-table of the rest, add a name key based on the URL, and put all this into JSON. That sounds big when I say, but is simple in PowerShell:

Awesome. Let’s try this now:

Paste this block of JSON into my Terminals settings fine, assign one of my entries the theme “Ubuntu” (or whatever your theme name is), and voila! here’s my command prompt with this theme. 

I’ve put the final script in my GitHub repo here in case it’s of use to anyone else. 

Update: The script has been updated to also allow one to specify an already downloaded iTerm2 colours file as input.