【问题标题】:Best way to integrate a Kivy label and icon集成 Kivy 标签和图标的最佳方式
【发布时间】:2018-03-11 10:34:54
【问题描述】:

我从https://github.com/iconic/open-iconic下载了一堆图标 图标最初为黑色,采用 .png 格式。

我想在我的一个画布上添加一个向上的箭头。

我找到了一种导入图标的方法,但问题是我无法更改图标颜色。我可以在 Kivy 中更改图标颜色,还是需要为要使用的每种颜色创建单独的 .png 图像?

<VitalBoard>:
canvas:
    Color:
        rgba: 0.17, 0.89, 0.89, 1
        hsv: 0.48, 0.80, 0.34
    Rectangle:
        pos: root.width * 2 / 3 + 20, root.height * 13 / 24 + 20
        size: root.width * 2 / 6 - 10 , root.height * 9 / 24 - 20
Label:
    font_size: 70
    text: "0"
    pos: root.width * 2 / 3 + 20, root.height * 13 / 24 + 20
    size: root.width * 2 / 6 - 10 , root.height * 9 / 24 - 20
    Image:
        source: 'open-iconic/png/arrow-thick-top-8x.png'
        pos: root.width * 2 / 3 + 20, root.height * 13 / 24 + 20
        size: root.width * 2 / 6 - 10 , root.height * 9 / 24 - 20
        width: 74

【问题讨论】:

  • 我认为您不能在 kivy 中更改图像颜色。在将它们加载到 kivy 之前,您必须在绘图程序中更改它们。

标签: python colors icons png kivy


【解决方案1】:

Kivy 在图像 https://kivy.org/docs/api-kivy.uix.image.html#kivy.uix.image.Image.color 上有一个颜色属性

似乎黑色和透明都没有被它改变。但是白色是可以改变的。

GridLayout:
    cols:4
    canvas.before:
        Color:
            rgba: [1,1,1,1]
        Rectangle:
            pos: self.pos
            size: self.size

    Image:
        source: 'arrow-bottom-8x.png'
    Image:
        source: 'arrow-bottom-8x.png'
        color: [1,0,0,1]
    Image:
        source: 'arrow-bottom-8x.png'
        color: [0,1,0,1]
    Image:
        source: 'arrow-bottom-8x.png'
        color: [0,0,1,1]
    Image:
        source: 'download.png'
    Image:
        source: 'download.png'
        color: [1,0,0,1]
    Image:
        source: 'download.png'
        color: [0,1,0,1]
    Image:
        source: 'download.png'
        color: [0,0,1,1]

我认为您需要手动或在 kivy 之外执行此操作。你可能想看看这里,例如https://stackoverflow.com/a/1616893/6646710

【讨论】:

  • 这是正确的,颜色是用来给像素着色的,通过乘法,所以当白色 (1, 1, 1, 1) 转换为给定的色调时,黑色 (0, 0, 0 , 1) 仅影响不透明度通道,透明 (X, X, X, 0) 不影响唯一重要的通道 alpha。如果你想给图标的黑色部分着色,可能值得在图像编辑器中创建一个反转版本,并在这个上使用颜色属性,并使用两个版本相互叠加,以创建颜色组合.
  • 这太棒了!由于许多图标都是黑色的,因此我将使用 imagemagick 为未来的观众添加此代码。 for i in ls *.png; do convert -negate ${i%.*}.png results/${i%.*}.png; done这会将图像从黑色反转为白色,以便我们可以使用PalimPalim提交的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 2010-10-15
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2010-11-30
  • 2023-04-05
相关资源
最近更新 更多