Skip to content

Commit

Permalink
Merge pull request #4 from wjmwjmwb/master
Browse files Browse the repository at this point in the history
change the divided difference calculation method
  • Loading branch information
LanceGin committed Aug 11, 2017
2 parents f7212ab + bb45aef commit 053d133
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
6 changes: 2 additions & 4 deletions demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
from haishoku.haishoku import Haishoku

def main():
path = "demo_01.png"
path = "/Users/wujianming/Desktop/WechatIMG18547.jpeg"
# path = "http://wx2.sinaimg.cn/large/89243dfbly1ffoekfainzj20dw05k0u7.jpg"

# getPalette api
palette = Haishoku.getPalette(path)
print(palette)

# getDominant api
dominant = Haishoku.getDominant(path)
print(dominant)

# showPalette api
Haishoku.showPalette(path)

# showDominant api
Haishoku.showDominant(path)
# Haishoku.showDominant(path)

# Haishoku object
h = Haishoku.loadHaishoku(path)
Expand Down
49 changes: 44 additions & 5 deletions haishoku/alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,52 @@ def sort_by_rgb(colors_tuple):
sorted_tuple = sorted(colors_tuple, key=lambda x:x[1])
return sorted_tuple

def rgb_maximum(colors_tuple):
"""
colors_r max min
colors_g max min
colors_b max min
"""
r_sorted_tuple = sorted(colors_tuple, key=lambda x:x[1][0])
g_sorted_tuple = sorted(colors_tuple, key=lambda x:x[1][1])
b_sorted_tuple = sorted(colors_tuple, key=lambda x:x[1][2])

r_min = r_sorted_tuple[0][1][0]
g_min = g_sorted_tuple[0][1][1]
b_min = b_sorted_tuple[0][1][2]

r_max = r_sorted_tuple[len(colors_tuple)-1][1][0]
g_max = g_sorted_tuple[len(colors_tuple)-1][1][1]
b_max = b_sorted_tuple[len(colors_tuple)-1][1][2]

return {
"r_max":r_max,
"r_min":r_min,
"g_max":g_max,
"g_min":g_min,
"b_max":b_max,
"b_min":b_min,
"r_dvalue":(r_max-r_min)/3,
"g_dvalue":(g_max-g_min)/3,
"b_dvalue":(b_max-b_min)/3
}

def group_by_accuracy(sorted_tuple, accuracy=3):
""" group the colors by the accuaracy was given
the R G B colors will be depart to accuracy parts
default accuracy = 3
[0, 85), [85, 170), [170, 256)
d_value = (max-min)/3
[min, min+d_value), [min+d_value, min+d_value*2), [min+d_value*2, max)
"""
rgb_maximum_json = rgb_maximum(sorted_tuple)
r_min = rgb_maximum_json["r_min"]
g_min = rgb_maximum_json["g_min"]
b_min = rgb_maximum_json["b_min"]
r_dvalue = rgb_maximum_json["r_dvalue"]
g_dvalue = rgb_maximum_json["g_dvalue"]
b_dvalue = rgb_maximum_json["b_dvalue"]

rgb = [
[[[], [], []], [[], [], []], [[], [], []]],
[[[], [], []], [[], [], []], [[], [], []]],
Expand All @@ -30,9 +69,9 @@ def group_by_accuracy(sorted_tuple, accuracy=3):
r_tmp_i = color_tuple[1][0]
g_tmp_i = color_tuple[1][1]
b_tmp_i = color_tuple[1][2]
r_idx = 0 if r_tmp_i < 85 else 1 if r_tmp_i < 170 else 2
g_idx = 0 if g_tmp_i < 85 else 1 if g_tmp_i < 170 else 2
b_idx = 0 if b_tmp_i < 85 else 1 if b_tmp_i < 170 else 2
r_idx = 0 if r_tmp_i < (r_min+r_dvalue) else 1 if r_tmp_i < (r_min+r_dvalue*2) else 2
g_idx = 0 if g_tmp_i < (g_min+g_dvalue) else 1 if g_tmp_i < (g_min+g_dvalue*2) else 2
b_idx = 0 if b_tmp_i < (b_min+b_dvalue) else 1 if b_tmp_i < (b_min+b_dvalue*2) else 2
rgb[r_idx][g_idx][b_idx].append(color_tuple)

return rgb
Expand Down

0 comments on commit 053d133

Please sign in to comment.