1
0
Fork 0
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:
lufebe16 2023-11-30 21:40:43 +01:00
parent 0afc62478e
commit fa0d0cf144
3 changed files with 48 additions and 21 deletions

View file

@ -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): def LColorToKivy(outline):
if (outline[0] == '#'): if (outline[0] == '#'):
outline = outline[1:] outline = outline[1:]

View file

@ -20,6 +20,7 @@ from kivy.properties import StringProperty
from kivy.uix.image import Image as KivyImage from kivy.uix.image import Image as KivyImage
from kivy.uix.widget import Widget from kivy.uix.widget import Widget
from pysollib.kivy.LApp import LColorToLuminance
from pysollib.kivy.LBase import LBase from pysollib.kivy.LBase import LBase
# ============================================================================= # =============================================================================
@ -221,4 +222,20 @@ class LImage(Widget, LBase):
def subsample(self, r): def subsample(self, r):
return LImage(texture=self.texture) 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)
# ============================================================================= # =============================================================================

View file

@ -30,10 +30,12 @@ import math
from kivy.clock import Clock from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.anchorlayout import AnchorLayout
from pysollib.kivy.LApp import LAnimationManager from pysollib.kivy.LApp import LAnimationManager
from pysollib.kivy.LApp import LColorToKivy from pysollib.kivy.LApp import LColorToKivy
from pysollib.kivy.LApp import LColorToLuminance
from pysollib.kivy.LApp import LImageItem from pysollib.kivy.LApp import LImageItem
from pysollib.kivy.LApp import LLine from pysollib.kivy.LApp import LLine
from pysollib.kivy.LApp import LRectangle from pysollib.kivy.LApp import LRectangle
@ -534,6 +536,10 @@ class MfxCanvasText(object):
self.canvas = canvas self.canvas = canvas
self.label = label self.label = label
self.widget = 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): def config(self, **kw):
# print('MfxCanvasText: config %s' % kw) # print('MfxCanvasText: config %s' % kw)
@ -567,6 +573,7 @@ class MfxCanvasText(object):
class MfxCanvas(LImage): class MfxCanvas(LImage):
_text_color = StringProperty("#000000")
def __str__(self): def __str__(self):
return f'<MfxCanvas @ {hex(id(self))}>' return f'<MfxCanvas @ {hex(id(self))}>'
@ -813,31 +820,21 @@ class MfxCanvas(LImage):
return -1 return -1
def setTextColor(self, color): def setTextColor(self, color):
# print('MfxCanvas: setTextColor1 %s' % color) # print('MfxCanvas: setTextColor')
if color is None: # color is ignored: it sets a predefined (option settable)
c = self.cget("bg") # color. We do not support that. Instead of this wie examine
if not isinstance(c, str) or c[0] != "#" or len(c) != 7: # the background and set the color accordingly.
return if self._bg_img is not None:
v = [] lumi = self._bg_img.luminance()
for i in (1, 3, 5): else:
v.append(int(c[i:i + 2], 16)) lumi = LColorToLuminance(self._bg_color)
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: setTextColor2 %s' % color) self._text_color = ("#000000", "#ffffff")[lumi < 0.5]
if self._text_color != color: print('average luminance =', lumi)
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)
def setTile(self, image, stretch=0, save_aspect=0): 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: if image:
try: try:
self._bg_img = LImage(source=image) self._bg_img = LImage(source=image)