UPDATE: Check out the latest details and code for this question below!
Note: This question focuses on optimizing the arrangement of items in a matrix, not discussing colors. Initially, I thought providing context would be helpful, but it ended up being too much color talk and not enough algorithm discussion. 😔
I have a pack of 80 unsorted felt tip pens for my child, and it's driving me crazy.
https://i.sstatic.net/dCnnY.png
I used to play Blendoku on Android where you arrange colors to form gradients with nearby colors being similar:
https://i.sstatic.net/6EHa0.gif
It's fun to organize colors like a crossword puzzle, but with these markers, I have a full 2D grid and non-uniform gradient colors.
I can't sort these pens by intuition; I need an algorithm!
Here's what I have:
- Firm grasp on JavaScript
- A flat array of color values for all pens
- A function
distance(color1, color2)
that measures color similarity from0
(identical) to100
All I'm missing is an algorithm.
With 80!
, brute forcing is unrealistic.
Possible approaches to make brute force workable:
- Set positions for some pens to reduce combinations
- Eliminate branches with highly dissimilar neighbors
- Stop after finding decent arrangement
But I still need an actual algorithm for this challenge.
PS Homework:
- Sorting a matrix by similarity -- unanswered
- Algorithm for optimal 2D color palette arrangement -- no answers
- How to sort colors in two dimensions? -- complex problem unsolved
- Sort Colour / Color Values -- single flat array
Update
Goal
Arrange 80 predefined colors in an 8×10 grid to create smooth gradients.
No definitive solution expected due to subjective nature of color perception.
Already have function for comparing colors.
Color space is 3D
Human eye sees colors in 3D trichromatic model.
Various models describe colors in 3D: RGB, HSL, HSV, etc.
Example palette:
https://i.sstatic.net/UY6D6.png
Hue vs saturation view lacks bright and dark colors.
This view misses many colors compared to full HSL/HSV space:
https://i.sstatic.net/ujLtC.png
Impossible to display all colors in gradient tear-free 2D layout.
E.g., lexicographically ordered RGB colors exhibit tearing:
https://i.sstatic.net/qNUmJ.png
My aim is good enough arrangement with mostly similar neighbors over perfect gradient.
Optimize grid in JS, not compare colors!
Using Delta E 2000 function to gauge color similarity, tailor each item pair adjacency for minimal difference.
Focus on optimization like mathematical functions.
Paths to tackle issue:
- genetic algorithm
- solver library
- manual sorting + algorithms
- other methods
Seeking JS solutions like CPLEX or Gurobi libraries, or genetic algo challenges like mating concepts.
Code snippet and color samples
JS code template available at: Click here
Setting instructions ensure effective operation.
Source data:
const data = [
{index: 1, id: "1", name: "Wine Red", rgb: "#A35A6E"},
{index: 2, id: "3", name: "Rose Red", rgb: "#F3595F"},
{index: 3, id: "4", name: "Vivid Red", rgb: "#F4565F"},
// ...
];
Index is color sequence based on ID order.
ID represents pen manufacturer number.
Color class.
Color abstraction simplifies color comparison.
index;
id;
name;
rgbStr;
collection;
constructor( {index, id, name, rgb}, collection ) {
// method implementations...
}
Collection class stores all colors and sorts them.
class Collection {
// Data goes here
constructor(data) {
// method implementations...
}
console.log(collection);
collection.render("colorsLinear");
Sample output shown in image below: