Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorear percentiles #129

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ colorama = "^0.4.6"
polib = "^1.2.0"
psutil = "^5.9.5"
pytz = "^2023.3"
pillow = "^10.0.0"
colour = "^0.1.5"

[tool.poetry.group.dev.dependencies]
poetry = "^1.5.1"
Expand Down
142 changes: 142 additions & 0 deletions tests/percentil_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from unogenerator import ODS_Standard
from unogenerator.commons import Coord as C
from decimal import Decimal
from PIL import ImageColor
from ast import literal_eval
from colour import Color


def hexstring_to_rgb(color_in_hex):
return ImageColor.getcolor(color_in_hex, "RGB")

def rgb_to_hexstring(rgb):
r = rgb[0]
g = rgb[1]
b = rgb[2]
return '#%02X%02X%02X' % (r,g,b)

def rgb_to_hex(rgb_tuple):
r = rgb_tuple[0]
g = rgb_tuple[1]
b = rgb_tuple[2]
s= '0x%02X%02X%02X' % (r,g,b)
# print(literal_eval(s))
# print("AJR", hex(literal_eval(s)))
return hex(literal_eval(s))
def rgb_to_int(rgb_tuple):
r = rgb_tuple[0]
g = rgb_tuple[1]
b = rgb_tuple[2]
s= '0x%02X%02X%02X' % (r,g,b)
# print(s, r, g, b)
# print("RGB_TO_INT", rgb_tuple, s, literal_eval(s))
# print("AJR", hex(literal_eval(s)))
return literal_eval(s)


def setCellBackgroundColor(doc, coord, color_int):
coord=C.assertCoord(coord)
# print("COLOR_INT", color_int)

cell=doc.sheet.getCellByPosition(coord.letterIndex(), coord.numberIndex())
cell.setPropertyValue("CellBackColor", color_int)

#def color(from_, to_, value):
# """
# Value entre 0 y 1
# """
# r1, g1, b1=from_
# r2, g2, b2=to_
#
## if r1>r2:
## r=r1-int((r1-r2)*value)
## else:
# r=r1+int(abs(r1-r2)*value)
## if g1>g2:
## g=g1-int((g1-g2)*value)
## else:
# g=g1+int(abs(g1-g2)*value)
## if b1>b2:
## b=b1-int((b1-b2)*value)
## else:
# b=b1+int(abs(b1-b2)*value)
#
# return r, g, b
#


def list_ordered_without_none(list_):
r=[]
for o in list_:
if o is None or o.__class__==str:
continue
r.append(o)
r.sort()
return r

def ordered_position_0_100(list_, item):
"""
Function List must bue ordered
"""
if item is None or item=="":
return 0
index=list_.index(item)
return int(100*index/len(list_))



def percentil_color(doc, letter, skip_up=0, skip_down=0, cast=Decimal):
"""
row_from and row_to are integers
"""
values=doc.getValuesByColumn(letter, skip_up=skip_up, skip_down=skip_down)
print(values, values.__class__)


# from_=(255, 0, 0)
# to_=(0, 255, 0)

#Cast all values
for i in range(len(values)):
if values[i] is None or values[i]=="":
continue
values[i]=cast(values[i])
print(values)
ordered_values=list_ordered_without_none(values)

#Calculate percentil
#colors=list(Color("green").range_to(Color("white"), 50))+ [Color("white")]+ list(Color("white").range_to(Color("red"), 50))
colors=list(Color("green").range_to(Color("red"), 101))
# Set colors
for i in range(len(values)):
coord=f"{letter}{i+skip_up+1}"
i_color_hex=colors[ordered_position_0_100(ordered_values, values[i])].get_hex()#
i_color_rgb=hexstring_to_rgb(i_color_hex)
print(i_color_rgb)

setCellBackgroundColor(doc, coord, rgb_to_int(i_color_rgb))


#################################
#
#rgb=hexstring_to_rgb("#23a9dd".upper())
#print(rgb)
#rrggbb=rgb_to_hexstring(rgb)
#print(rrggbb)
#print(rgb_to_hex(rgb))
#print("MIDDLE")
#middle=color((255, 0, 0), (0, 0, 0), 0.5)
#print(middle)
#print( rgb_to_hex(middle), rgb_to_int(middle))

Red=0xFF9999
Green=0xc0FFc0

with ODS_Standard() as doc:
doc.createSheet("Get Values")
doc.addColumnWithStyle("A1", [1, 2, 3, 4, "=A4+1", 6, 7, None, 9, 10])
percentil_color(doc, "A")
doc.createSheet("Get Values2")
doc.addColumnWithStyle("A1", range(100))
percentil_color(doc, "A")
doc.export_pdf("percentil_color.pdf")
4 changes: 3 additions & 1 deletion unogenerator.epj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"header": {
"comment": "eric project file for project unogenerator",
"copyright": "Copyright (C) 2023 , ",
"saved": "2023-07-12, 08:04:29"
"saved": "2023-08-24, 14:49:34"
},
"project": {
"AUTHOR": "",
Expand Down Expand Up @@ -52,6 +52,7 @@
},
"MIXEDLANGUAGE": false,
"OTHERS": [
"DOCKER.md",
"INSTALL.md",
"README.md",
"unogenerator.epj"
Expand All @@ -73,6 +74,7 @@
"tests/benchmark.py",
"tests/get_values.py",
"tests/metadata.py",
"tests/percentil_color.py",
"tests/pregunta.py",
"tests/pruebasetcolors.py",
"tests/range.py",
Expand Down