133 lines
4.3 KiB
Python
133 lines
4.3 KiB
Python
import numpy as np
|
|
from PyQt5.QtWidgets import QMdiArea
|
|
|
|
def rgb2hsv(rgb):
|
|
""" convert RGB to HSV color space
|
|
|
|
:param rgb: np.ndarray
|
|
:return: np.ndarray
|
|
"""
|
|
rgb = rgb.astype('float')
|
|
maxv = np.amax(rgb, axis=2)
|
|
maxc = np.argmax(rgb, axis=2)
|
|
minv = np.amin(rgb, axis=2)
|
|
minc = np.argmin(rgb, axis=2)
|
|
|
|
hsv = np.zeros(rgb.shape, dtype='float')
|
|
hsv[maxc == minc, 0] = np.zeros(hsv[maxc == minc, 0].shape)
|
|
hsv[maxc == 0, 0] = (((rgb[..., 1] - rgb[..., 2]) * 60.0 / (maxv - minv + np.spacing(1))) % 360.0)[maxc == 0]
|
|
hsv[maxc == 1, 0] = (((rgb[..., 2] - rgb[..., 0]) * 60.0 / (maxv - minv + np.spacing(1))) + 120.0)[maxc == 1]
|
|
hsv[maxc == 2, 0] = (((rgb[..., 0] - rgb[..., 1]) * 60.0 / (maxv - minv + np.spacing(1))) + 240.0)[maxc == 2]
|
|
hsv[maxv == 0, 1] = np.zeros(hsv[maxv == 0, 1].shape)
|
|
hsv[maxv != 0, 1] = (1 - minv / (maxv + np.spacing(1)))[maxv != 0]
|
|
hsv[..., 2] = maxv
|
|
|
|
return hsv
|
|
|
|
def hsv2rgb(hsv):
|
|
""" convert HSV to RGB color space
|
|
|
|
:param hsv: np.ndarray
|
|
:return: np.ndarray
|
|
"""
|
|
hi = np.floor(hsv[..., 0] / 60.0) % 6
|
|
hi = hi.astype('uint8')
|
|
v = hsv[..., 2].astype('float')
|
|
f = (hsv[..., 0] / 60.0) - np.floor(hsv[..., 0] / 60.0)
|
|
p = v * (1.0 - hsv[..., 1])
|
|
q = v * (1.0 - (f * hsv[..., 1]))
|
|
t = v * (1.0 - ((1.0 - f) * hsv[..., 1]))
|
|
|
|
rgb = np.zeros(hsv.shape)
|
|
rgb[hi == 0, :] = np.dstack((v, t, p))[hi == 0, :]
|
|
rgb[hi == 1, :] = np.dstack((q, v, p))[hi == 1, :]
|
|
rgb[hi == 2, :] = np.dstack((p, v, t))[hi == 2, :]
|
|
rgb[hi == 3, :] = np.dstack((p, q, v))[hi == 3, :]
|
|
rgb[hi == 4, :] = np.dstack((t, p, v))[hi == 4, :]
|
|
rgb[hi == 5, :] = np.dstack((v, p, q))[hi == 5, :]
|
|
|
|
return rgb
|
|
|
|
|
|
def get_qview(view):
|
|
window = view.window()
|
|
if window is None:
|
|
return
|
|
qwindow = window.qwindow()
|
|
if qwindow is None:
|
|
return
|
|
mdi_area = qwindow.centralWidget()
|
|
if mdi_area:
|
|
mdi_area = qwindow.centralWidget().findChild(QMdiArea)
|
|
for kis_view, sub_win in zip(window.views(), mdi_area.subWindowList()):
|
|
if view == kis_view:
|
|
return next(c for c in sub_win.children() if c.metaObject().className() == 'KisView')
|
|
else:
|
|
mdi_area = qwindow.findChild(QMdiArea)
|
|
for kis_view, sub_win in zip(window.views(), mdi_area.subWindowList()):
|
|
if view == kis_view:
|
|
return next(c for c in sub_win.children() if c.metaObject().className() == 'KisView')
|
|
|
|
def dump_tablet_event(self, tablet_event):
|
|
text = (
|
|
f'=============================================\n'
|
|
f'{tablet_event}\n'
|
|
' from QEvent\n'
|
|
f' {tablet_event.type()=}\n'
|
|
f' {tablet_event.spontaneous()=}\n'
|
|
' from QInputEvent\n'
|
|
f' {tablet_event.timestamp()=}\n'
|
|
f' {tablet_event.modifiers()=}\n'
|
|
' from QTabletEvent\n'
|
|
f' {tablet_event.button()=}\n'
|
|
f' {tablet_event.buttons()=}\n'
|
|
f' {tablet_event.device()=}\n'
|
|
f' {tablet_event.deviceType()=}\n'
|
|
f' {tablet_event.globalPos()=}\n'
|
|
f' {tablet_event.globalPosF()=}\n'
|
|
f' {tablet_event.globalX()=}\n'
|
|
f' {tablet_event.globalY()=}\n'
|
|
f' {tablet_event.hiResGlobalX()=}\n'
|
|
f' {tablet_event.hiResGlobalY()=}\n'
|
|
f' {tablet_event.pointerType()=}\n'
|
|
f' {tablet_event.pos()=}\n'
|
|
f' {tablet_event.posF()=}\n'
|
|
f' {tablet_event.pressure()=}\n'
|
|
f' {tablet_event.rotation()=}\n'
|
|
f' {tablet_event.tangentialPressure()=}\n'
|
|
f' {tablet_event.uniqueId()=}\n'
|
|
f' {tablet_event.x()=}\n'
|
|
f' {tablet_event.xTilt()=}\n'
|
|
f' {tablet_event.y()=}\n'
|
|
f' {tablet_event.yTilt()=}\n'
|
|
f' {tablet_event.z()=}\n'
|
|
)
|
|
print(text)
|
|
|
|
def dump_mouse_event(self, mouse_event):
|
|
text = (
|
|
f'{mouse_event}\n'
|
|
' from QEvent\n'
|
|
f' {mouse_event.type()=}\n'
|
|
f' {mouse_event.spontaneous()=}\n'
|
|
' from QInputEvent\n'
|
|
f' {mouse_event.timestamp()=}\n'
|
|
f' {mouse_event.modifiers()=}\n'
|
|
' from QMouseEvent\n'
|
|
f' {mouse_event.button()=}\n'
|
|
f' {mouse_event.buttons()=}\n'
|
|
f' {mouse_event.flags()=}\n'
|
|
f' {mouse_event.globalPos()=}\n'
|
|
f' {mouse_event.globalX()=}\n'
|
|
f' {mouse_event.globalY()=}\n'
|
|
f' {mouse_event.localPos()=}\n'
|
|
f' {mouse_event.pos()=}\n'
|
|
f' {mouse_event.screenPos()=}\n'
|
|
f' {mouse_event.source()=}\n'
|
|
f' {mouse_event.windowPos()=}\n'
|
|
f' {mouse_event.x()=}\n'
|
|
f' {mouse_event.y()=}\n'
|
|
)
|
|
print(text)
|
|
|