Skip to content

Commit

Permalink
Kivy/Android
Browse files Browse the repository at this point in the history
- Text color adaptation to current background settings.
  • Loading branch information
lufebe16 committed Dec 4, 2023
1 parent 0afc624 commit fa0d0cf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
13 changes: 13 additions & 0 deletions pysollib/kivy/LApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down
17 changes: 17 additions & 0 deletions pysollib/kivy/LImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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

# =============================================================================
Expand Down Expand Up @@ -221,4 +222,20 @@ def getWidth(self):
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)

# =============================================================================
41 changes: 19 additions & 22 deletions pysollib/kivy/tkcanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@


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
Expand Down Expand Up @@ -534,6 +536,10 @@ def __init__(self, canvas, x, y, preview=-1, **kwargs):
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)
Expand Down Expand Up @@ -567,6 +573,7 @@ def addtag(self, tag):


class MfxCanvas(LImage):
_text_color = StringProperty("#000000")

def __str__(self):
return f'<MfxCanvas @ {hex(id(self))}>'
Expand Down Expand Up @@ -813,31 +820,21 @@ def findCard(self, stack, event):
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: 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)
# 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)

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)
Expand Down

0 comments on commit fa0d0cf

Please sign in to comment.