1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

Kivy/Android

- refactoring
This commit is contained in:
lufebe16 2024-01-05 17:56:57 +01:00
parent a35f0ad2f5
commit 87fe16413e
5 changed files with 78 additions and 54 deletions

View file

@ -91,12 +91,7 @@ class Images:
except Exception: except Exception:
return None return None
if TOOLKIT == 'kivy': w, h = img.width(), img.height()
w = img.texture.size[0]
h = img.texture.size[1]
else:
w, h = img.width(), img.height()
if self.CARDW < 0: if self.CARDW < 0:
self.CARDW, self.CARDH = w, h self.CARDW, self.CARDH = w, h
else: else:

View file

@ -270,6 +270,25 @@ def LColorToLuminance(color):
# ============================================================================= # =============================================================================
def LTextureToLuminance(texture):
b = 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)
# =============================================================================
def LColorToKivy(outline): def LColorToKivy(outline):
if (outline[0] == '#'): if (outline[0] == '#'):
outline = outline[1:] outline = outline[1:]

View file

@ -20,7 +20,6 @@ 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
# ============================================================================= # =============================================================================
@ -222,20 +221,4 @@ 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

@ -35,7 +35,6 @@ 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
@ -856,8 +855,10 @@ class MfxCanvas(LImage):
# color. We do not support that. Instead of this wie examine # color. We do not support that. Instead of this wie examine
# the background and set the color accordingly. # the background and set the color accordingly.
if self._bg_img is not None: if self._bg_img is not None:
lumi = self._bg_img.luminance() from pysollib.kivy.LApp import LTextureToLuminance
lumi = LTextureToLuminance(self._bg_img.texture)
else: else:
from pysollib.kivy.LApp import LColorToLuminance
lumi = LColorToLuminance(self._bg_color) lumi = LColorToLuminance(self._bg_color)
self._text_color = ("#000000", "#ffffff")[lumi < 0.4] self._text_color = ("#000000", "#ffffff")[lumi < 0.4]
@ -868,7 +869,8 @@ class MfxCanvas(LImage):
# print('setTile: %s, %s, %s' % (image, stretch, save_aspect)) # print('setTile: %s, %s, %s' % (image, stretch, save_aspect))
if image: if image:
try: try:
self._bg_img = LImage(source=image) from pysollib.kivy.tkutil import LImageInfo
self._bg_img = LImageInfo(image)
self._stretch_bg_image = stretch self._stretch_bg_image = stretch
self._save_aspect_bg_image = save_aspect self._save_aspect_bg_image = save_aspect
self.update_widget(self.pos, self.size) self.update_widget(self.pos, self.size)

View file

@ -37,7 +37,6 @@ from kivy.core.text import Label as CoreLabel
from kivy.graphics.texture import Texture from kivy.graphics.texture import Texture
from pysollib.kivy.LApp import LTopLevel0 from pysollib.kivy.LApp import LTopLevel0
from pysollib.kivy.LImage import LImage
# ************************************************************************ # ************************************************************************
# * window manager util # * window manager util
@ -216,39 +215,64 @@ def after_cancel(t):
# ************************************************************************ # ************************************************************************
# * image handling # * image handling
# ************************************************************************ # ************************************************************************
# Wrappers
LCoreImage = CoreImage # noqa
class LImageInfo(object): # noqa
def __init__(self, arg):
if type(arg) is Texture:
self.filename = None
self.source = None
self.texture = arg
self.size = self.texture.size
if type(arg) is str:
self.filename = arg
self.source = arg
self.texture = LCoreImage(arg).texture
self.size = self.texture.size
# pysol core needs that:
def subsample(self, image):
return self
def width(self):
return self.size[0]
def height(self):
return self.size[1]
def getWidth(self):
return self.size[0]
def getHeight(self):
return self.size[1]
# ************************************************************************
# Interface to core.
def makeImage(file=None, data=None, dither=None, alpha=None): def makeImage(file=None, data=None, dither=None, alpha=None):
kw = {}
if data is None: if data is None:
assert file is not None assert file is not None
kw["source"] = file return LImageInfo(file)
# print('makeImage: source = %s' % file)
# if (file=='/home/lb/PRG/Python/Kivy/pysolfc/data/images/redeal.gif'):
# y = self.yy
else: else:
assert data is not None assert data is not None
kw["texture"] = data return LImageInfo(data)
# ob das geht ?? - kommt das vor ?
# yy = self.yy
'''
if 'source' in kw:
logging.info ("makeImage: " + kw["source"])
if 'texture' in kw:
logging.info ("makeImage: " + str(kw["texture"]))
'''
return LImage(**kw) loadImage = makeImage # noqa - sorry flake8, aber das gehört zu oben dazu!
loadImage = makeImage
def copyImage(image, x, y, width, height): def copyImage(image, x, y, width, height):
# return Image(source=image.source) # wird das überhaupt aufgerufen - ja bei SubSampleImage.
# return Image(texture=image.texture) # aber: wo wird das gebraucht? - oder ist es eine altlast
return image # welche gar keine Relevanz mehr hat ? (Kann auch None
# zurückgeben, ohne dass etwas fehlt oder abstürzt).
tregion = image.texture.get_region(x, y, width, height)
return LImageInfo(tregion)
def fillTexture(texture, fill, outline=None, owidth=1): def fillTexture(texture, fill, outline=None, owidth=1):
@ -337,14 +361,16 @@ def createImage(width, height, fill, outline=None, outwidth=1):
texture = Texture.create(size=(width, height), colorfmt='rgba') texture = Texture.create(size=(width, height), colorfmt='rgba')
fillTexture(texture, fill, outline, outwidth) fillTexture(texture, fill, outline, outwidth)
image = LImage(texture=texture) image = LImageInfo(texture)
# logging.info("createImage: LImage create %s" % image) # logging.info("createImage: LImageInfo create %s" % image)
return image return image
def createImagePIL(width, height, fill, outline=None, outwidth=1): def createImagePIL(width, height, fill, outline=None, outwidth=1):
# Is this needed for Kivy? # Is this needed for Kivy?
createImage(width, height, fill, outline=outline, outwidth=outwidth) createImage(width, height, fill, outline=outline, outwidth=outwidth)
# wird nur mit USE_PIL benutzt: nicht relevant. Der code wird mit
# Kivy nie durchlaufen.
def shadowImage(image, color='#3896f8', factor=0.3): def shadowImage(image, color='#3896f8', factor=0.3):
@ -488,15 +514,14 @@ def createBottom(image, color='white', backfile=None):
tmp0 = _createImageMask(image.texture, color) tmp0 = _createImageMask(image.texture, color)
if backfile: if backfile:
tmp1 = CoreImage(backfile) tmp1 = LCoreImage(backfile)
txtre = _scaleTextureToSize(tmp1.texture, image.texture.size) txtre = _scaleTextureToSize(tmp1.texture, image.texture.size)
tmp = _pasteTextureTo(txtre, tmp0) tmp = _pasteTextureTo(txtre, tmp0)
else: else:
tmp = tmp0 tmp = tmp0
img = LImage(texture=tmp) img = LImageInfo(tmp)
img.size[0] = image.getWidth() img.size = (image.getWidth(), image.getHeight())
img.size[1] = image.getHeight()
return img return img
''' '''
im = image._pil_image im = image._pil_image