# rubiks-color-resolver: convert RGB values of each square to U,L,F,R,B or D



## dwalton76 (Mar 3, 2017)

If you have analyzed an image of a rubiks cube or used a robot to scan each square you have the RGB values for each square but need to assign each square one of the size possible sides/colors of the cube. I started on working on this for MINDCUB3R but since then I have been using it for other rubiks cube solving projects.

The code is available on github
https://github.com/dwalton76/rubiks-color-resolver

Example: Assume that we scanned a cube and the RGB values (as viewed in a web page) are:






To translate those RGB values into 54 ULFRBD values the represent the cube we run rubiks-color-resolver.py and pass it a json string that contains those RGB values.


```
rubiks-color-resolver.py '{"6": [17, 123, 101], "16": [235, 254, 250], "44": [130, 5, 3], "15": [179, 212, 11], "1": [13, 47, 136], "54": [17, 109, 96], "9": [27, 54, 143], "40": [138, 4, 5], "31": [242, 249, 255], "47": [8, 38, 128], "22": [216, 52, 0], "3": [13, 50, 139], "19": [170, 9, 0], "23": [169, 6, 7], "28": [210, 208, 2], "21": [159, 5, 3], "30": [196, 192, 0], "51": [6, 36, 124], "25": [167, 4, 5], "37": [192, 56, 4], "12": [227, 253, 254], "46": [12, 104, 93], "52": [25, 116, 109], "34": [201, 197, 1], "50": [18, 110, 99], "7": [31, 70, 165], "18": [236, 252, 252], "10": [235, 254, 250], "8": [19, 131, 107], "48": [17, 109, 96], "45": [193, 45, 5], "43": [185, 48, 4], "14": [230, 255, 255], "38": [140, 4, 6], "2": [15, 127, 102], "39": [181, 48, 5], "32": [201, 197, 1], "49": [13, 42, 136], "33": [238, 245, 253], "26": [210, 43, 9], "53": [11, 40, 134], "13": [187, 218, 26], "4": [19, 130, 111], "36": [203, 200, 1], "20": [217, 51, 0], "11": [186, 218, 23], "24": [213, 47, 5], "29": [242, 251, 248], "5": [18, 57, 152], "41": [190, 47, 5], "42": [131, 6, 4], "17": [177, 208, 16], "27": [168, 5, 6], "35": [238, 245, 253]}'
```

Which outputs 





The UDUDUDUDURLRLRLRLRFBFBFBFBFDUDUDUDUDLRLRLRLRLBFBFBFBFB string is in the order expected by the kociemba 3x3x3 solver. 

This works as follows:

Identify the six colors used by the cube. In this case the colors are red, orange, green, blue, yellow and white.
Create a list of the color combinations used by all edges (red/blue, yellow/red, etc)
Create a list of the color combinations used by all corners (white/orange/green, red/blue/white, etc)
Create a list of centers color squares
For each edge compute the color distance of that edge vs. all of the edges that in the "list of edge color combinations that we need" and find the one that is the closest match. The distance between two colors is calculated using the CIEDE2000 algorithm.
Repeat the process for the corners and centers
The end result is that we can nail down exactly which side each square belongs to. 

rubiks-color-resolver.py will work for any size cube, I have tested with up to 6x6x6. Feel free to use it if you are processing RGB values of rubiks cube squares.


----------

