使用Python和Scribus创建一个RGB立方体

 使用Python和Scribus创建一个RGB立方体

使用 Scribus 的 Python 脚本编写器功能,开发一个显示 RGB 色谱的 3D 立方体。

当我决定这个夏天要玩色彩游戏时,我想到通常色彩都是在色轮上描绘的。这些色彩通常都是使用色素而不是光,并且你失去了任何对颜色亮度或光度变化的感觉。

作为色轮的替代,我想在立方体表面使用一系列图形来显示 RGB 频谱。色彩的 RGB 值将在具有 X、Y、Z 轴的三维图形上展示。例如,一个平面将会保持 B(蓝色)为 0,其余的坐标轴将显示当我将 R(红色)和 G (绿色)的值从 0 绘制到 255 时发生的情况。

事实证明,使用 Scribus 及其 Python 脚本编写器 功能实现这一点并不困难。我可以创建 RGB 颜色,使矩形显示颜色,并以 2D 格式排列它们。我决定设置颜色值的间隔为 5,并让矩形按 5 个点(pt)进行绘图。因此,对于每个 2D 图形,我将使用大约 250 种颜色,立方体的一个边有 250 个点(pt),也就是 3.5 英寸。

我使用下面这段 Python 代码完成了绿 - 红图的任务:

  1. <span class="pln">x </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span>
  2. <span class="pln">y </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span>
  3. <span class="pln">r </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  4. <span class="pln">g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  5. <span class="pln">b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  6.  
  7. <span class="kwd">if</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">newDoc</span><span class="pun">(</span><span class="pln">scribus</span><span class="pun">.</span><span class="pln">PAPER_LETTER</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">),</span><span class="pln">scribus</span><span class="pun">.</span><span class="pln">PORTRAIT</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln">                  scribus</span><span class="pun">.</span><span class="pln">UNIT_POINTS</span><span class="pun">,</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">NOFACINGPAGES</span><span class="pun">,</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">FIRSTPAGERIGHT</span><span class="pun">):</span>
  8. <span class="pln">    </span><span class="kwd">while</span><span class="pln"> r </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  9. <span class="pln">        </span><span class="kwd">while</span><span class="pln"> g </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  10. <span class="pln">            newcolor </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">r</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">g</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">b</span><span class="pun">)</span>
  11. <span class="pln">            </span><span class="kwd">if</span><span class="pln"> newcolor </span><span class="pun">==</span><span class="pln"> </span><span class="str">'0_0_0'</span><span class="pun">:</span>
  12. <span class="pln">                newcolor </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Black'</span>
  13. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">defineColorRGB</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln">r</span><span class="pun">,</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">)</span>
  14. <span class="pln">            rect </span><span class="pun">=</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">createRect</span><span class="pun">(</span><span class="pln">x </span><span class="pun">+</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">)</span>
  15. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setFillColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  16. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setLineColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  17. <span class="pln">            g </span><span class="pun">=</span><span class="pln"> g </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  18. <span class="pln">        g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  19. <span class="pln">        r </span><span class="pun">=</span><span class="pln"> r </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  20. <span class="pln">        y </span><span class="pun">=</span><span class="pln"> y </span><span class="pun">–</span><span class="pln"> </span><span class="lit">5</span>

这个脚本在 300,300 位置开始绘制图形,这个位置大约是一个美国信件大小的纸张的水平中心,大概是垂直方向从顶部到底的三分之一位置;这是图像的原点,然后它沿着 X 轴(绿色值)水平构建图形,然后返回到 Y 轴,向上移动 5 个点,然后绘制下一条矩形线。

使用Python和Scribus创建一个RGB立方体

Red-Green graph

这看起来很简单;我只需要调整一下数字就可以把立方体的另一面画出来。但这不仅仅是再画两个图,一个是蓝 - 绿色,另一个是红 - 蓝色的问题。我想创建一个展开的立方体,这样我就可以打印、剪开然后折叠它,创建一个 RGB 的 3D 视图。因此,下一部分(向下的页面)的原点(黑色的角落)需要在左上角,其水平方向是绿色,垂直方向是蓝色。

“调整数字”最终或多或少变成了试错,从而得到我想要的东西。在创建了第二个图之后,我需要第三个图,它是红 - 蓝色的,原点位于左上角,红色向左递增,蓝色向下递增。

下面是最终效果图:

使用Python和Scribus创建一个RGB立方体

First half of RGB cube

当然,这只是这个立方体的前半部分。我需要做一个类似的形状,除了原点应该是白色(而不是黑色)来表示高值。这是我希望自己更聪明的时候之一,因为我不仅需要做出一个类似的整体形状,还需要以镜像的方式与第一个形状交互(我认为)。有时候,尝试和错误是你唯一的朋友。

结果是这样的;我使用了一个单独的脚本,因为在一个美国信件大小的页面上没有足够的空间同时容纳这两个图案。

使用Python和Scribus创建一个RGB立方体

Second half of RGB cube

现在,是时候轮到打印机了!在这里,你可以直观了解彩色打印机如何处理 RGB 颜色到 CMYK 颜色的转换以及打印颜色密集空间。

接下来,朋友们,是剪切粘贴时间!我可以用胶带,但我不想改变表面的外观,所以我在切割的时候在两边留下了一些空间,这样我就可以把它们粘在里面了。根据我的经验,在复印纸上打印会产生一些不需要的皱纹,所以在我的复印纸原型完成后,我把立方体打印在了更厚的纸上,表面是哑光的。

使用Python和Scribus创建一个RGB立方体

RGB cubes

请记住,这只是 RGB 空间边界的一个视图;更准确地说,你必须做出一个可以在中间切片的实心立方体。例如,这是一个实心 RGB 立方体在蓝色 = 120 的切片。

使用Python和Scribus创建一个RGB立方体

RGB cube slice

最后,我做这个项目很开心。如果您也想参与其中,这里有两个脚本。

这是前半部分:

  1. <span class="com">#</span><span class="pun">!</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">env</span><span class="pln"> python</span>
  2. <span class="com">#</span><span class="pln"> black2rgb</span><span class="pun">.</span><span class="pln">py</span>
  3. <span class="str">"""</span>
  4. <span class="str">Creates one-half of RGB cube with Black at origin</span>
  5. <span class="str">"""</span>
  6.  
  7. <span class="kwd">import</span><span class="pln"> scribus</span>
  8.  
  9. <span class="pln">x </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span>
  10. <span class="pln">y </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span>
  11. <span class="pln">r </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  12. <span class="pln">g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  13. <span class="pln">b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  14.  
  15. <span class="kwd">if</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">newDoc</span><span class="pun">(</span><span class="pln">scribus</span><span class="pun">.</span><span class="pln">PAPER_LETTER</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">),</span><span class="pln">scribus</span><span class="pun">.</span><span class="pln">PORTRAIT</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">UNIT_POINTS</span><span class="pun">,</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">NOFACINGPAGES</span><span class="pun">,</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">FIRSTPAGERIGHT</span><span class="pun">):</span>
  16. <span class="pln">    </span><span class="kwd">while</span><span class="pln"> r </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  17. <span class="pln">        </span><span class="kwd">while</span><span class="pln"> g </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  18. <span class="pln">            newcolor </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">r</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">g</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">b</span><span class="pun">)</span>
  19. <span class="pln">            </span><span class="kwd">if</span><span class="pln"> newcolor </span><span class="pun">==</span><span class="pln"> </span><span class="str">'0_0_0'</span><span class="pun">:</span>
  20. <span class="pln">                newcolor </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Black'</span>
  21. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">defineColorRGB</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln">r</span><span class="pun">,</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">)</span>
  22. <span class="pln">            rect </span><span class="pun">=</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">createRect</span><span class="pun">(</span><span class="pln">x </span><span class="pun">+</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">)</span>
  23. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setFillColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  24. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setLineColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  25. <span class="pln">            g </span><span class="pun">=</span><span class="pln"> g </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  26. <span class="pln">        g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  27. <span class="pln">        r </span><span class="pun">=</span><span class="pln"> r </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  28. <span class="pln">        y </span><span class="pun">=</span><span class="pln"> y </span><span class="pun">-</span><span class="pln"> </span><span class="lit">5</span>
  29. <span class="pln">       </span>
  30. <span class="pln">    r </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  31. <span class="pln">    g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  32. <span class="pln">    y </span><span class="pun">=</span><span class="pln"> </span><span class="lit">305</span>
  33.  
  34. <span class="pln">    </span><span class="kwd">while</span><span class="pln"> b </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  35. <span class="pln">        </span><span class="kwd">while</span><span class="pln"> g </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  36. <span class="pln">            newcolor </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">r</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">g</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">b</span><span class="pun">)</span>
  37. <span class="pln">            </span><span class="kwd">if</span><span class="pln"> newcolor </span><span class="pun">==</span><span class="pln"> </span><span class="str">'0_0_0'</span><span class="pun">:</span>
  38. <span class="pln">                newcolor </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Black'</span>
  39. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">defineColorRGB</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln">r</span><span class="pun">,</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">)</span>
  40. <span class="pln">            rect </span><span class="pun">=</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">createRect</span><span class="pun">(</span><span class="pln">x </span><span class="pun">+</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">)</span>
  41. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setFillColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  42. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setLineColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  43. <span class="pln">            g </span><span class="pun">=</span><span class="pln"> g </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  44. <span class="pln">        g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  45. <span class="pln">        b </span><span class="pun">=</span><span class="pln"> b </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  46. <span class="pln">        y </span><span class="pun">=</span><span class="pln"> y </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  47. <span class="pln">       </span>
  48. <span class="pln">    r </span><span class="pun">=</span><span class="pln"> </span><span class="lit">255</span>
  49. <span class="pln">    g </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  50. <span class="pln">    y </span><span class="pun">=</span><span class="pln"> </span><span class="lit">305</span>
  51. <span class="pln">    x </span><span class="pun">=</span><span class="pln"> </span><span class="lit">39</span>
  52. <span class="pln">    b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span>
  53.  
  54. <span class="pln">    </span><span class="kwd">while</span><span class="pln"> b </span><span class="pun"><</span><span class="pln"> </span><span class="lit">256</span><span class="pun">:</span>
  55. <span class="pln">        </span><span class="kwd">while</span><span class="pln"> r </span><span class="pun">>=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">:</span>
  56. <span class="pln">            newcolor </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">r</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">g</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'_'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">b</span><span class="pun">)</span>
  57. <span class="pln">            </span><span class="kwd">if</span><span class="pln"> newcolor </span><span class="pun">==</span><span class="pln"> </span><span class="str">'0_0_0'</span><span class="pun">:</span>
  58. <span class="pln">                newcolor </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Black'</span>
  59. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">defineColorRGB</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln">r</span><span class="pun">,</span><span class="pln"> g</span><span class="pun">,</span><span class="pln"> b</span><span class="pun">)</span>
  60. <span class="pln">            rect </span><span class="pun">=</span><span class="pln"> scribus</span><span class="pun">.</span><span class="pln">createRect</span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">)</span>
  61. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setFillColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  62. <span class="pln">            scribus</span><span class="pun">.</span><span class="pln">setLineColor</span><span class="pun">(</span><span class="pln">newcolor</span><span class="pun">,</span><span class="pln"> rect</span><span class="pun">)</span>
  63. <span class="pln">            r </span><span class="pun">=</span><span class="pln"> r </span><span class="pun">-</span><span class="pln"> </span><span class="lit">5</span>
  64. <span class="pln">            x </span><span class="pun">=</span><span class="pln"> x</span><span class="pun">+</span><span class="lit">5</span>
  65. <span class="pln">        b </span><span class="pun">=</span><span class="pln"> b </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  66. <span class="pln">        x </span><span class="pun">=</span><span class="pln"> </span><span class="lit">39.5</span>
  67. <span class="pln">        r </span><span class="pun">=</span><span class="pln"> </span><span class="lit">255</span>
  68. <span class="pln">        y </span><span class="pun">=</span><span class="pln"> y </span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span>
  69. <span class="pln">       </span>
  70. <span class="pln">scribus</span><span class="pun">.</span><span class="pln">setRedraw</span><span class="pun">(</span><span class="kwd">True</span><span class="pun">)</span>
  71. <span class="pln">scribus</span><span class="pun">.</span><span class="pln">redrawAll</span><span class="pun">()</span>

后半部分:

相关推荐