mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-05 00:02:29 -04:00
Kivy/Android
- Text color adaptation to current background settings.
This commit is contained in:
parent
0afc62478e
commit
fa0d0cf144
3 changed files with 48 additions and 21 deletions
|
@ -248,6 +248,19 @@ def addAnchorOffset(pos, size, anchor):
|
|||
# =============================================================================
|
||||
|
||||
|
||||
def LColorToLuminance(color):
|
||||
kc = color
|
||||
if isinstance(color, str):
|
||||
kc = LColorToKivy(color)
|
||||
r = kc[0]
|
||||
g = kc[1]
|
||||
b = kc[2]
|
||||
Y = 0.2989*r + 0.5866*g + 0.1145*b
|
||||
return Y
|
||||
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def LColorToKivy(outline):
|
||||
if (outline[0] == '#'):
|
||||
outline = outline[1:]
|
||||
|
|
|
@ -20,6 +20,7 @@ from kivy.properties import StringProperty
|
|||
from kivy.uix.image import Image as KivyImage
|
||||
from kivy.uix.widget import Widget
|
||||
|
||||
from pysollib.kivy.LApp import LColorToLuminance
|
||||
from pysollib.kivy.LBase import LBase
|
||||
|
||||
# =============================================================================
|
||||
|
@ -221,4 +222,20 @@ class LImage(Widget, LBase):
|
|||
def subsample(self, r):
|
||||
return LImage(texture=self.texture)
|
||||
|
||||
def luminance(self):
|
||||
b = self.texture.pixels
|
||||
s = 4
|
||||
n = len(b)/1000
|
||||
if n > 4:
|
||||
s = n - n % 4
|
||||
n = 0
|
||||
ll = 0
|
||||
for i in range(0, len(b), int(s)):
|
||||
rr = int.from_bytes(b[i:i+1], byteorder='big', signed=False) / 256.0 # noqa
|
||||
gg = int.from_bytes(b[i+1:i+2], byteorder='big', signed=False) / 256.0 # noqa
|
||||
bb = int.from_bytes(b[i+2:i+3], byteorder='big', signed=False) / 256.0 # noqa
|
||||
ll = ll + LColorToLuminance([rr, gg, bb, 1])
|
||||
n += 1
|
||||
return (ll/n)
|
||||
|
||||
# =============================================================================
|
||||
|
|
|
@ -30,10 +30,12 @@ import math
|
|||
|
||||
|
||||
from kivy.clock import Clock
|
||||
from kivy.properties import StringProperty
|
||||
from kivy.uix.anchorlayout import AnchorLayout
|
||||
|
||||
from pysollib.kivy.LApp import LAnimationManager
|
||||
from pysollib.kivy.LApp import LColorToKivy
|
||||
from pysollib.kivy.LApp import LColorToLuminance
|
||||
from pysollib.kivy.LApp import LImageItem
|
||||
from pysollib.kivy.LApp import LLine
|
||||
from pysollib.kivy.LApp import LRectangle
|
||||
|
@ -534,6 +536,10 @@ class MfxCanvasText(object):
|
|||
self.canvas = canvas
|
||||
self.label = label
|
||||
self.widget = label
|
||||
self.canvas.bind(_text_color=self.setColor)
|
||||
|
||||
def setColor(self, w, c):
|
||||
self.label.label.color = LColorToKivy(c)
|
||||
|
||||
def config(self, **kw):
|
||||
# print('MfxCanvasText: config %s' % kw)
|
||||
|
@ -567,6 +573,7 @@ class MfxCanvasText(object):
|
|||
|
||||
|
||||
class MfxCanvas(LImage):
|
||||
_text_color = StringProperty("#000000")
|
||||
|
||||
def __str__(self):
|
||||
return f'<MfxCanvas @ {hex(id(self))}>'
|
||||
|
@ -813,31 +820,21 @@ class MfxCanvas(LImage):
|
|||
return -1
|
||||
|
||||
def setTextColor(self, color):
|
||||
# print('MfxCanvas: setTextColor1 %s' % color)
|
||||
if color is None:
|
||||
c = self.cget("bg")
|
||||
if not isinstance(c, str) or c[0] != "#" or len(c) != 7:
|
||||
return
|
||||
v = []
|
||||
for i in (1, 3, 5):
|
||||
v.append(int(c[i:i + 2], 16))
|
||||
luminance = (0.212671 * v[0] + 0.715160 *
|
||||
v[1] + 0.072169 * v[2]) / 255
|
||||
# print c, ":", v, "luminance", luminance
|
||||
color = ("#000000", "#ffffff")[luminance < 0.3]
|
||||
# print('MfxCanvas: setTextColor')
|
||||
# color is ignored: it sets a predefined (option settable)
|
||||
# color. We do not support that. Instead of this wie examine
|
||||
# the background and set the color accordingly.
|
||||
if self._bg_img is not None:
|
||||
lumi = self._bg_img.luminance()
|
||||
else:
|
||||
lumi = LColorToLuminance(self._bg_color)
|
||||
|
||||
# print('MfxCanvas: setTextColor2 %s' % color)
|
||||
if self._text_color != color:
|
||||
self._text_color = color
|
||||
|
||||
# falls wir das wollen in kivy:
|
||||
# -> text_color als property deklarieren, und a.a.O binden.
|
||||
# for item in self._text_items:
|
||||
# item.config(fill=self._text_color)
|
||||
self._text_color = ("#000000", "#ffffff")[lumi < 0.5]
|
||||
print('average luminance =', lumi)
|
||||
|
||||
def setTile(self, image, stretch=0, save_aspect=0):
|
||||
|
||||
print('setTile: %s, %s' % (image, stretch))
|
||||
# print('setTile: %s, %s, %s' % (image, stretch, save_aspect))
|
||||
if image:
|
||||
try:
|
||||
self._bg_img = LImage(source=image)
|
||||
|
|
Loading…
Add table
Reference in a new issue