如何在Kivy中将文本颜色设置为渐变纹理? [英] How to set text color to gradient texture in kivy?
问题描述
我曾经用渐变色创建过一个Texture
,并将其设置为Label
,Button
等的背景.但是我想知道如何将其设置为Label
的color
? >
I have used to create a Texture
with gradient color and set to the background of Label
, Button
and etc. But I am wondering how to set this to color
of Label
?
推荐答案
听起来很有趣.所以这是我想出的:
Sounded like a fun puzzle. So here is what I came up with:
from kivy.lang import Builder
from kivy.graphics.texture import Texture
from kivy.properties import ObjectProperty, ListProperty
from kivy.uix.label import Label
class LabelGradient(Label):
gradient = ObjectProperty(None)
bg_color = ListProperty([0, 0, 0, 255])
def __init__(self, **kwargs):
super(LabelGradient, self).__init__(**kwargs)
# bind to texture to trigger use of gradient
self.bind(texture=self.fix_texture)
def fix_texture(self, instance, texture):
if self.gradient is None:
return
# unbind, so we don't loop
self.unbind(texture=self.fix_texture)
# The normal Label texture is transparent except for the text itself
# This code changes the texture to make the text transparent, and everything else
# gets set to self.bg_color (a property of LabelGradient)
pixels = list(self.texture.pixels)
for index in range(3, len(pixels)-4, 4):
if pixels[index] == 0:
# change transparent pixels to the bg_color
pixels[index-3:index+1] = self.bg_color
else:
# make the text itself transparent
pixels[index] = 0
# create a new texture, blit the new pixels, and apply the new texture
new_texture = Texture.create(size=self.texture.size, colorfmt='rgba')
new_texture.blit_buffer(bytes(pixels), colorfmt='rgba', bufferfmt='ubyte')
new_texture.flip_vertical()
self.texture = new_texture
Builder.load_string('''
<LabelGradient>:
canvas.before:
# draw the gradient below the normal Label Texture
Color:
rgba: 1,1,1,1
Rectangle:
texture: self.gradient
size: self.texture_size
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.)
''')
if __name__ == '__main__':
from kivy.app import App
from kivy.lang import Builder
class LabelGradientApp(App):
grad = ObjectProperty(None)
def build(self):
# create a 64x64 texture, defaults to rgba / ubyte
self.grad = Texture.create(size=(64, 64))
# create 64x64 rgb tab, and fill with values from 0 to 255
# we'll have a gradient from black to white
size = 64 * 64 * 3
buf = bytes([int(x * 255 / size) for x in range(size)])
# then blit the buffer
self.grad.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
return theRoot
app = LabelGradientApp()
theRoot = Builder.load_string('''
FloatLayout:
LabelGradient:
text: 'Abba Dabba Doo'
font_size: 30
gradient: app.grad
''')
app.run()
我的方法是使文本透明,并在文本后面绘制渐变.
由Label
创建的普通Texture
是透明的,除了实际文本之外.
My approach is to make the text transparent, and to draw a gradient behind the text.
The normal Texture
created by the Label
is transparent except for the actual text.
上面的LabelGradient
扩展了Label
并使用kv
中的<LabelGradient>
规则绘制了渐变. fix_texture()
方法绑定到Label
的texture
属性,因此在创建Texture
后立即将其调用. fix_texture()
方法使实际文本透明,并将Texture
的其余部分设置为bg_color
.
The LabelGradient
above extends Label
and draws the gradient using the <LabelGradient>
rule in the kv
. The fix_texture()
method is bound to the texture
property of the Label
, so it is called as soon as the Texture
is created. The fix_texture()
method makes the actual text transparent and sets the rest of the Texture
to the bg_color
.
这篇关于如何在Kivy中将文本颜色设置为渐变纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!