[Question]
  [
[Part 2 of the challenge is here](https://codegolf.stackexchange.com/questions/196335/country-name-mashup-generator)
[Flags Mashup Bot](https://twitter.com/flagsmashupbot) is a small Twitter bot that generates a new flag based on two random flags and tweets the result every couple of minutes. 
## Task
Your task is to replicate what the bot does by writing a script or a function based on the following criteria:
* The input is any two lossless images composed of at least two, but no more than eight colours.
* To mix up the flags the following algorithm has to be used:
	+ Take the first image
	+ For every colour in the first image assign a colour randomly from the second image
	+ Each colour from the second image has to be used at least once
		- Unless there are less colours in the first image than colours in the second.
		- You can choose randomly which colours to drop from the second image in this case.
	+ Each colour from the first image has to be replaced with a different colour
		- Unless there are not enough colours in the second image.
		- You can choose randomly which colours will be used multiple times in this case.
		- However you need to make sure none of the colours are used more than any other colour by more than one
		- example: if you need to assign 2 colours to a flag of 5 colours you'll need to use one of the colour 3 times, while the other one 2 times. You cannot mix them 1 & 4
	+ If a colour is present in both images you cannot assign that colour with itself you'll need to pick another one
* The output is the resulting image
## Examples
**UK & Hungary**
[![Example 1](https://i.stack.imgur.com/nQEaU.png)](https://i.stack.imgur.com/nQEaU.png)
Notes: Both flags have three colours, however both use red and white, so those have to be replaced, hence the first invalid example. The second one doesn't use all three colours, and the third one uses non-existent colours.
**South Africa & Sweden**
[![Example 2](https://i.stack.imgur.com/FvaXp.png)](https://i.stack.imgur.com/FvaXp.png)
Notes: South Africa's flag has 6 colours, while Sweden's have 2, meaning each of the two colours have to be used exactly 3 times. The invalid ones use the colours in a way which is not compatible with this constraint. Note that although both flags use the colour yellow they are actually not the same shade so are considered different
**Poland & Poland**
[![Example 3](https://i.stack.imgur.com/gE02M.png)](https://i.stack.imgur.com/gE02M.png)
Notes: The only valid solution is switching up the two colours.
## Notes
* The two images don't need to have the same dimensions
* The colour list can be different for the two images, so in total there might be 16 colours
* The input can be either a lossless image file (example GIF, PNG, BMP, SVG, etc.), or (especially in case of functions) a two dimensional bitmap array holding the colours of the image in any reasonable format. Any native way of storing a bitmap in your language is also fine.
* For both input and output images a two dimensional array holding ASCII characters, where different characters count as different colours, counts as a reasonable format, but solutions that work on "real" bitmaps are preferred
* The output and input formats don't need to match
* I think the colour replacement rules won't prohibit it to work for any valid two input images, but if yes your program doesn't need to work for them
* Colours are considered the same only if they are exactly the same based on their RGB value. E.g. `#FF0000` and `#FF0001` are considered two different colours.
* Standard loopholes, as usual, are prohibited
[code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code by byte count wins and will be accepted. If possible please include example a set of input & output images with your submission
  
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~160~~ 159 bytes
I/O format: matrix of 24-bit integers.
Takes input as `(a)(b)`.
```
a=>F=b=>(g=a=>[...new Set(a.flat())].sort(_=>Math.random()-.5))(a).every((c,i)=>c^=F[c]=B[i%B.length],B=g(b).slice(-g(a).length))?a.map(r=>r.map(c=>F[c])):F(b)
```
[Try it online!](https://tio.run/##vZLPS8MwFMfv@ytykb0H3aMe3EFIhQm5eRqeSpW3LO0qXVPSMPWvr@mqbFM22FBzCB/yvu9H8s0Lb7jVrmz8pLZL0@WyY5kouZAJFDJgSkS1eRVz44Epr9gDYkatdR6eZfLAfkWO66VdA07oBhEYyWyMewfQUYky0U9SpTqTs7S8mlFl6sKvsmgmC1ggtVWpDUyKPmsIId4xrbkBJxO3BR0GCgUQb1VI6bStW1sZqmwBIyFySMMuRBq/xWEpFYlDUiqOd5RFl6mV@orsU5@5ozPVQZzhbvr@dJjiOx3W/anp177maJ1tx7AN7ytkIj5fuEdN3s69K@sCrqdIDS/nnoPL00iM4zGG6GPTGHfPrQlfAEc4Ou7Ef93ldMdTbp6n/hvvf82J7gM "JavaScript (Node.js) – Try It Online")
### Commented
**Helper function**
The helper function \$g\$ takes a matrix as input and returns a shuffled list of the distinct values it holds.
```
g = a =>
  [...new Set(a.flat())]
  .sort(_ => Math.random() - .5)
```
**Main code**
```
a => F = b =>                 // a[] = first image, b[] = second image
  g(a).every((c, i) =>        // for each color c at position i in the list of shuffled
                              // colors of the first image:
    c ^=                      //   make sure that c is different from the
      F[c] = B[i % B.length], //     substitute color F[c] picked from the second image
    B =                       //   define B[] as:
      g(b)                    //     the list of shuffled colors from the second image
      .slice(-g(a).length)    //     resized to the number of colors in the first image
  ) ?                         // end of every(); if truthy:
    a.map(r =>                //   return a[] with each original color
      r.map(c => F[c])        //   replaced with its substitute color
    )                         //
  :                           // else:
    F(b)                      //   try again
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~40~~ 25 bytes
```
FQ),ṁ@¥/Ẋ€⁺ZE€ẸƊ¿iⱮⱮ@ịƭƒḢ
```
[Try it online!](https://tio.run/##7dfBSgMxEAbg@76JENBtX6AXvXtU6bEHpS/gsb0I9eYjCN5U8NauB6HCvkf2RdINpcbdnSQzqdil/Eyh0E0mH0vSzNxNptN7Yy4uT5RezUbrl1NdLKr5azUrrs/rb10sy8X667b6eK8/I/35WL6VT3r5bL4froy5yerI1aAVZ9HYjst/hWymmzsYK0twKbiMNEJneRuW0PqJxZATyOUdgXwUTCojeJffESJDyMR8QjT3lsAamkBg5XQEGSNOYOdqEvhTwwTRG@0SeCn8BPG@ogkcBk1IOF3uUKaEjyAKEHaEXB00QAChZwT8L/SEEL6mQldPjNB@5r2m5MtzLms2w1@yhFPwShYWgy7c4lP5hVs0V7d85b1AWfkazNks4vnbSF7Ee3N3DyVrFye2MoGGTrb8fg0d2dZKl9@/rW0w6Lb2P5r7H8Y4swhc2CCAAAIIR9hL/EmAAAIIIPSUMFQHDRBAAAEEEEhCZmMD "Jelly – Try It Online")
A monadic link taking a list of two flags as integer matrices and returning the altered version of the first flag. At the moment this assumes there are indexed colours representing each pixel. I've also assumed that each flag can have arbitrary colours.
[Answer]
# [Python 2](https://docs.python.org/2/), 196 bytes
Takes input as two lists of lists, where different elements represent different colors. Anything can be used to represent color: number, hex, string, etc.
```
from random import*
P=input()
i,j=(list(set(sum(x,[])))for x in P)
d={0:0}
while any(i==d[i]for i in d):y=[];exec'y+=sample(j,len(j));'*len(i);d=dict(zip(i,y))
print[[d[c]for c in r]for r in P[0]]
```
[Try it online!](https://tio.run/##JY0xT8MwEIX3/ApvtssNIWyNvJR2A1IJFnTyUMWuelHiWI4rEhC/PThGT6f36d7TnV/ibXTVul7DOLBwcSYZDX4McVecFTl/j0IWBJ0SPU1RTDbNfRAzoJZSXsfAZkaOnWVh1E@5L3@Lrxv1ll3cIkgpg6S3Em0lI/eLQl3b2bZ8eVDTZfC9FR301olOyprvNiJZG2WojeKbvCBYpCx8IBcRDbb5XLudCxlDfo@l1uuKWEKWBnyErEQVZCV6gqxEZcqqRBoYY4j8vXk98bTnh@b4yYE3b8@nZB/NyzHZf5Ze/AE "Python 2 – Try It Online")
---
Explanation:
```
# get unique elements from input
P=input()
i,j=(list(set(sum(x,[])))for x in P)  
# create dictionary, where colors of first image are keys and colors of second image are values
d={0:0}
# keep creating while no color is paired with same color
while any(i==d[i]for i in d):
  y=[]
  # sum different samples of substitution colors to keep random
  # and to make sure none of the colors are used more than any other color by more than one
  # *len(i) is to ensure that we have enough values to match
  exec'y+=sample(j,len(j));'*len(i)
  d=dict(zip(i,y))
# substitute colors and output result
print[[d[c]for c in r]for r in P[0]]
```
[Answer]
# [Perl 5](https://www.perl.org/), ~~170~~ 168 bytes
```
sub f{($a,$b,$e)=map[uniq(/./g)],@_;%n=map{($_,0)}@$b;%s=map{$c=$$b[rand@$b];$e+=/$c/;$n{$c}++;($_,$c)}@$a;@v=values%n;max(@v)-min(@v)>1||$e?f(@_):$_[0]=~s,.,$s{$&},gr}
```
[Try it online!](https://tio.run/##zZNRa9swEMff8ylunpJKsyrbgZYuwpspjL5sL4FhRhqC7MiON1t2rThNSLyPXld2NhiFscdVLyfd/aW7H7qrZJ1fdZ1uIkiOGAmKIook8QtRLRqVPWCHOSlZ0mDFx6r3GtGKuqQNUMTHevCg2EcoWtRCrY13yZG0fQfFDkfKxFrb5v0dFPeXBA92/k7kjdRjxQuxx8GOXBaZ6u0H73RC8mOCgxWZodXCXfo/NWUU6SOatDSt2073WbBH@Cgpa@wxNnXJcQRmFQeMklykHh3MlPhpLaujw5gzmbxxMCPs3b3nnE61XJdtX/f3MlMXF9RsId7U@PrKPj9@TQh1GRsON6Ttk/AhxcsED4/4Lpx/u43g04FAlgBa@b53w@GtLpvtBkRSZ7GACehHuZYK5F4UVS7hf66/gczDW5iHd8REf4O8NyDND1P@plGpqA@vov5/goRzCOdkkPwCmboGpCpz85@vC@EPkOIAgfaHiev7MXkBRk0Teq7rknMjVnWmtmANEv@shEF4PkwhOC5cO9DLFnSZN9usVHoGFus7Hltg0UATZt0ri4/a7qmsBkF3@eVzprez2ddtlg@lUDOXZjz2zw "Perl 5 – Try It Online")
With spaces, newlines, `my`'s, comments and a small test script added:
```
sub f{
  my($a,$b,$e)=map[uniq(/./g)],@_; #$a and $b are now arrayrefs to the unique
                                   #  colors in each flag
  my %n=map{($_,0)}@$b;          #init %n with 0 values for each col in flag2
  my %s=map{                     #%s becomes a suggested swap hash
    $c=$$b[rand@$b];             #draw random color from flag2
    $e += /$c/;                  #short for: $error=true if $_ eq $c
    $n{$c}++;                    #count usages of each flag2 color
    ($_,$c)                      #suggest swap this col in flag1 with random
                                 #  col in flag2
  }
  @$a;
  my @v=values%n;
  max(@v)-min(@v)>1              #no more than one more usages of each flag2 color
  || $e                          #cannot swap with itself
  ?  f(@_)                       #retry if %s don't passes
  :  $_[0]=~s,.,$s{$&},gr        #swap colors with %s if %s passes the demands
}
srand(1);
for(1..20){
    my($flag1,$flag2)=
      grep{/../&&!/(.).*\1/||redo}
      map{join'',map chr(65+rand(16)),0..rand(8)}
      1..2;
    ($flag1,$flag2)=qw(GWRYBb Ey) if $_==18; #south africa & sweden example                                                                                                                             
    ($flag1,$flag2)=qw(RWB RWG)   if $_==19; #uk & hungary example                                                                                                                                      
    ($flag1,$flag2)=qw(WR WR)     if $_==20; #poland example                                                                                                                                            
    my @s=uniq(map f($flag1,$flag2),1..1000);
    print "flag1=$flag1 flag2=$flag2 @{[0+@s]} solutions: ".join(" ",@s)."\n";
}
use List::Util qw(uniq min max);
```
The image format consists of up to sixteen different capital letters A-P for each pixel. The letter represents a color in a color map (like in gifs). Max eight unique colors in each flag. The `f`unction returns the first argument ($a) where each letter is (potentially) swapped with a letter in the second argument ($b) through the swap hash `%s`. The `%n` hash is used to count the number of colers each swapped in color is used for and the least frequent (can be zero) shouldn't be less than one below the max frequency. Also `$e` (error) becomes true if `f` attempts to swap a color with itself in which case `f` is also run again.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 70 bytes
```
ＷＳ⟦ι⟧ＷΦＫＡ¬№υκ⊞υ‽ι≔υθ≔⟦⟧ηＷＳＦιＦ¬№ηκ⊞ηκＷ⊙υ⁼κ§θλ«≔⟦⟧θＷΦη¬№θλ⊞θ‽κ»ＵＭＫＡ§θ⌕υι
```
[Try it online!](https://tio.run/##7VDBagMhED3HrxjIRYv5gtDDEhrIoSU0x5CDJDbKuprd1bSl9Nut7pZdGwh4XehD0BnfvDczR8Gao2HK@3chFQe80Rdnd7aR@owJgW14WLyXB7JEv4y1VJY3eMt5WSiFCYUXY/HKuEB0FEpCYp1rRYxemT6ZCksS6ou2lWcds/UY7Q8UxCh@Y/9mGgjF/T3aiN6md@mCQaDQn9HhqXZMtbikUNiNPvEPXFNQXWtfaJZ4x1ZmfycT6URDWedVDxOVcaJv9MwuK1NVIZcuJPFcy/AVGgobIEvvHwDSM78LBDfUu0D/mlmauUDzXExGEy1yMRlm/pqmwnzMxWSYfnFVPw "Charcoal – Try It Online") Link is to verbose version of code. Takes input as character arrays, where different characters represent different colours. For the link I've rendered two flags in ASCII art. Explanation:
```
ＷＳ⟦ι⟧
```
Input and print the first character array.
```
ＷΦＫＡ¬№υκ⊞υ‽ι
```
Randomly extract the distinct characters into a list.
```
≔υθ
```
Make a temporary copy of the list. This copy will be replaced by a shuffle of the transliteration characters.
```
≔⟦⟧η
```
Create a second list. (The first list was automatically predefined by Charcoal.)
```
ＷＳＦιＦ¬№ηκ⊞ηκ
```
Input the second character array and extract the unique characters to the second list.
```
Ｗ⊙υ⁼κ§θλ«
```
Repeat while at least one character in the first list maps to a character in the second list. The mapping is cyclic, so that once all of the colours run out, they are reused again in the same order, thus ensuring that no colour is used an unbalanced number of times.
```
≔⟦⟧θＷΦη¬№θλ⊞θ‽κ
```
Shuffle the unique characters from the second list.
```
»ＵＭＫＡ§θ⌕υι
```
Once a distinct shuffle has been found, transliterate the whole canvas from the first to the second list.
]