最近感觉自己论文里面的图示配色很难看,受到了这篇文章的启发,我也做了一个莫兰迪风格的颜色盘 ,放在下面 gist 里面。参考的文章里面的色盘主要有两个问题,一个是单个色盘可选颜色的数量很少,另一个是很难决定在需要 K 个颜色的情况下应该选哪几个颜色,选不好的话区分度会很差。

https://gist.github.com/ChuanyuXue/3a377f7c1629b0ce68bc6b393340d0fb

👇下面是拿来做图的结果:

针对颜色数量不足等问题,我把颜色的数量扩展到了 18 个。针对如何选择颜色的问题,我分别搞了两种选择的方式,一种是直接选择多个颜色,另一种是选择多组颜色。


1. 引言:

首先讲一下为什么我要搞这个色盘,主要有两个原因:第一个是我一直觉得 Giorgio Morandi 的风格很好,有些安静和低调的气质(虽然后面才知道这个颜色已经被用烂了😓)。另一个是我一直想统一下自己的绘图风格,虽然我大部分时间喜欢黑白画图,但是很多情况下还是需要用一些彩色去做区分。所以最后决定拿莫兰迪色做一套自用的色盘。

最开始我也在网上搜了一些类似的色盘,其实色彩搭配都不错,很有莫兰迪色的感觉。比如像下面这种:

但是问题是选择那种颜色合适还是一个比较纠结的问题(强迫症难受 🫥。就像上面这个图有 32 种颜色, 如果我只需要 8 种颜色的话,该怎么选比较好。

2. 方法:

下面就是具体选择颜色的方法,我分了两种场景,一种是需要选择多种颜色,另一种是需要把颜色分成多个组。

我写了一些比较具体的证明,都放在引用块里,如果不感兴趣可以跳过… 💨

2.1 场景一:

假设我们色盘里有 N 种颜色,但是我们要选择 K 个颜色,怎么选是最优的? 

定义最优:首先我们需要定义什么叫最优。因为这个色盘是用来做图的,所以最重要的是选择的颜色需要有区分度。所以我们把最优定义成选择的 K 个颜色之间的距离最大化。因为每个颜色都是由三维 RGB 定义的,我们可以用三维空间中的距离来代表颜色之间的距离 🔍。

选择颜色:这里我们用一个比较直观的方法去选择颜色。我们首先随机选择第一个颜色,然后每一步添加一个颜色。在选择添加哪个颜色时,我们就选择可以最大化当前选择集合的距离的颜色。比如下面的例子:

最后选择完的颜色(基本用法就是需要几个颜色的时候,就从前往后选几个):

最后,我们证明这个颜色选择的问题是无法在多项式时间内求解的(实际无意义,毕竟就这么几种颜色 🤡

The feasibility version of the problem can be formalized as: 

Define: Given a set of colors U={C1,C2,...,CN} , where the distance between colors in RGB space is represented as d(Ci,Cj) . The problem is: Does there exist a subset of colors in size K such that the average distance between them is at least D . Next, we show the clique problem is reducible to this problem. Clique problem is defined as given undirected graph G=(V,E) and an integer L , determine if G contains a clique (complete subgraph) of size at least L . Given any instance of clique problem, we construct the color selecting problem as follows: Let each color Ci∈U represent corresponding vertex vi∈V , where |U|=|V| . And let the distance d(Ci,Cj)=δ if there exist link ei,j∈E , otherwise set d(Ci,Cj)=γ<δ . The construction of colors is polynomial because solving quadratic equation is polynomial. 

Theorem 1: If the color selecting problem has a solution with a average distance D=(L−1)δ/2 with L colors, then the graph G contains a clique of size L.

Proof: In the solution, consider there are x pairs of distance in δ and y pairs <δ . The average distance is calculated as D=(δx+γy)/L=(L−1)δ/2 . By definition we have x+y=L(L−1)/2 , we notice that it only happens when x=L(L−1)/2 . Since the maximum number of links of any subgraph of G with size L is also L(L−1)/2 , thus subgraph must be a clique.◻

后面我再看这个问题的时候,发现因为 RGB 颜色是整数,而且是从 0 - 255 有限的数字,所以其实存在动态规划算法可以实现 pseudo-polynomial 的复杂度。我觉得我现在的算法也够用了,所以如果感兴趣的话可以看看下面这个新论文:

Eremeev, A. V., Kel’manov, A. V., Kovalyov, M. Y., & Pyatkin, A. V. (2019). Maximum diversity problem with squared Euclidean distance. In Mathematical Optimization Theory and Operations Research: 18th International Conference, MOTOR 2019, Ekaterinburg, Russia, July 8-12, 2019, Proceedings 18_ (pp. 541-551). Springer International Publishing.

2.2 场景二:

第二个场景是我们需要把颜色分成几个组去用。这也是个很常见的场景,比如我们有海鲜和家禽两类数据,用实线和虚线区分,然后具体的产品再用颜色区分 🦞。

我们的目标是让每个分组内的距离最大化,这个问题可以描述成一个整形线性规划的问题,然后拿求解器去求解。下面是用这个方法分组的一个样例:

下面是线性规划问题的描述:

然后我们证明这个问题也是个 NP 问题:

We prove the problem is NP-complete by reducing 3D-matching problem to it. Let X,Y,Z be a set of R integers, and let T be a subset of X×Y×Z . Now M⊆T is a 3-dimensional matching if the following holds: for any two distinct triples from M , non of their elements disjointed. Given the 3D-matching problem, we construct the color partition problem as following: Create M=R groups and each group with size Am=3 . Create N=3R colors, where the first R colors as Ci=[xi,y^i,z^i] where xi∈X , and so as other 2R colors. If 3 colors Ci,Cj,Ck satisfy [xi,yj,zk]∈T , we set d(Ci,Cj)=d(Cj,Ck)=d(Ci,Ck)=δ , otherwise we set d(Ci,Cj)=γ<δ if there is no such Ck . 

Theorem 1: If color partition problem has sum of distance δ|M| , then there exist a 3D-matching solution M . 

Since there are δ|M| distance and |M| groups, then the average distance in each group is δ . For each group Am=3 there are also 3 pairs of distance, thus each pair of distance must be at least δ . ◻

2.3 获得色盘颜色

前面在讨论的都是选择颜色的问题,还有一个需要解决的问题是如何获得最初的色盘颜色。我一共尝试了两种方案:

  • 使用自定义色盘:上面用的这些颜色是我直接从网上找的,然后选择一些我觉得比较有代表性的颜色拼凑起来的。
  • 直接从画里面提取颜色:另外我还试了直接从莫兰迪的画里面直接提取颜色。 我在网上找了 22 副他比较好看的画,然后拿一个 Python 程序去从里面提取颜色。下面是我提取的结果:

不过我感觉提取的这些颜色有点暗了,可能不太适合做图 😇

下面是我选的 22 个画:

用来提取颜色的 Python 程序的伪代码 (原始代码在 gist 里面):

Create an empty color list L
For each image in the folder: 
	Get the number of colors with pixel counts (C, K)
	If there exist color C' in L, that d(C', C) is too close:
	    continue to next color
	Else:
		Add color C into L 
Pick the top 18 most common colors and return

具体用哪一种色盘看具体需求吧,我自己感觉原汁原味提取的颜色反而不好…… 🙅

3. 结论

希望这个色盘可以有用吧。我自己的感觉是在自己的论文里面用自己设计的颜色,还是不错的感觉哈哈哈哈哈 😝。然后其实不光 Morandi 颜色,这一套方法也可以迁移到其他艺术家身上,推荐大家可以拿自己喜欢的艺术家来试试 ✨。