From 076c844563b0b927811557ce5657260c861699af Mon Sep 17 00:00:00 2001 From: Gael-de-Sailly Date: Sun, 30 Jul 2017 21:37:15 +0200 Subject: [PATCH] Created repository. - 256 variants of a single item "256_dyes:dye". Not useful for now. - Universal mixing craft recipe: you can mix up to 9 colors at once on the crafting grid. - In game, only the brown dye can be found in creative inventory. You can change the color by clicking: - Right click changes hue - Left click changes brightness - The `color.dat` file is used by the mixing algorithm. (compressed list of RGB color codes) - gen_palette.py: Python 3 script to run with Python Image Library to generate the palette and color.dat. Could be easily modified to generate a different palette. --- colors.dat | Bin 0 -> 380 bytes gen_palette.py | 77 +++++++++++++++++++ init.lua | 132 ++++++++++++++++++++++++++++++++ mod.conf | 1 + textures/256_dyes_dye.png | Bin 0 -> 325 bytes textures/256_dyes_palette_1.png | Bin 0 -> 432 bytes textures/256_dyes_palette_2.png | Bin 0 -> 380 bytes 7 files changed, 210 insertions(+) create mode 100644 colors.dat create mode 100644 gen_palette.py create mode 100644 init.lua create mode 100644 mod.conf create mode 100644 textures/256_dyes_dye.png create mode 100644 textures/256_dyes_palette_1.png create mode 100644 textures/256_dyes_palette_2.png diff --git a/colors.dat b/colors.dat new file mode 100644 index 0000000000000000000000000000000000000000..d54653a6551270dcb916333f5ee8322b00bd0789 GIT binary patch literal 380 zcmV-?0fYW{oK4ZOjlw__1kfn~IzZwaGzU+jJxP0dAUlZgfDocEta#c* z8t6RVLjyb^j93^?#oL~xkuG^PBsjMsA4Vv?KepKugD@EmEWu~!9iFXGEM`Yg7z_nU z(4l9zSc52YMRY=MFfay7XbDf&UL2e!|G^9@kFdFd3lM`nm8AJu60dvgv;k;ooD;N#< zTW_w}?FYQ7q#kbSjY-~DQdfsgQd4h!lg)pI?!f_?D(PEq%<@Q;8l|9G;6L~`Xn%J5 z>Y1VeTeCQtrFv)#(SWT|3(NvmLYI+61GXk{pNRliuh*N+X1m?)cDuvja6BGQr_=d- azFaQX>-BcK-S79u= 240 then + if color == 255 then + new_color = 240 + else + new_color = color + 1 + end + else + variation = color % 10 + local hue = color - variation + new_color = (variation+1) % 10 + hue + end + meta:set_int("palette_index", new_color) + return itemstack +end + +local function change_hue(itemstack) + local meta = itemstack:get_meta() + local color = meta:get_int("palette_index") + local new_color = color + 10 + if color >= 230 then + if color == 255 then + new_color = 0 + else + new_color = math.max(color + 1, 240) + end + end + meta:set_int("palette_index", new_color) + return itemstack +end + +minetest.register_craftitem("256_dyes:dye", { + description = "Dye", + groups = {}, + inventory_image = "256_dyes_dye.png", + wield_image = "256_dyes_dye.png", + palette = "256_dyes_palette_2.png", + stack_max = 99, + color = "#400000", + on_use = change_variation, + on_place = change_hue, + on_secondary_use = change_hue, +}) + +local file = io.open(modpath .. "/colors.dat", "r") +local raw_colors = minetest.decompress(file:read("*all")) +file:close() + +local colors = {} +for i=0, 255 do + local pos = i * 3 + raw_color = raw_colors:sub(pos+1, pos+3) + color = {raw_color:byte(1, 3)} + colors[i] = color +end + +local function mix(...) + local list = {...} + local n = #list + local sum_c = 0 + local sum_m = 0 + local sum_y = 0 + for _, i in ipairs(list) do + r, g, b = unpack(colors[i]) + local c, m, y = 255-r, 255-g, 255-b + sum_c = sum_c + c^2 + sum_m = sum_m + m^2 + sum_y = sum_y + y^2 + end + local true_color = {math.sqrt(sum_c/n), math.sqrt(sum_m/n), math.sqrt(sum_y/n)} -- I found that calculating the quadratic mean of CMY colors gives more realistic mixes because it makes the mix more sensitive to dark colors + + local min_diff = 765 + local nearest = 0 + + for i=0, 255 do + local color = colors[i] + local c, m, y = 255-color[1], 255-color[2], 255-color[3] + local diff = math.abs(c - true_color[1]) + math.abs(m - true_color[2]) + math.abs(y - true_color[3]) + if diff < min_diff then + min_diff = diff + nearest = i + end + end + return nearest +end + +for i=1, 9 do + recipe = {} + for j=1, i do + table.insert(recipe, "256_dyes:dye") + end + + minetest.register_craft({ + type = "shapeless", + output = "256_dyes:dye", + recipe = recipe, + }) +end + +local function dye_craft(itemstack, player, old_craft_grid, craft_inv) + if itemstack:get_name() ~= "256_dyes:dye" then + return + end + local list_colors = {} + for _, stack in ipairs(old_craft_grid) do + if not stack:is_empty() then + if stack:get_name() == "256_dyes:dye" then + local meta = stack:get_meta() + table.insert(list_colors, meta:get_int("palette_index")) + else + return + end + end + end + if #list_colors == 0 then + return + end + itemstack = ItemStack("256_dyes:dye " .. #list_colors) + local new_color = mix(unpack(list_colors)) + local meta = itemstack:get_meta() + meta:set_int("palette_index", new_color) + return itemstack +end + +minetest.register_craft_predict(dye_craft) +minetest.register_on_craft(dye_craft) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..eb5d6aa --- /dev/null +++ b/mod.conf @@ -0,0 +1 @@ +name = 256_dyes diff --git a/textures/256_dyes_dye.png b/textures/256_dyes_dye.png new file mode 100644 index 0000000000000000000000000000000000000000..7cfc3578b90ab2836d35bf6d983f5c542d4dc2e4 GIT binary patch literal 325 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPtyBxbEZ(2a*PN2{wPZ!4!i_>=}8S))6;Bk!?T%Pda z)PWNd3V7BzT<4qnfTw`nEX{)L+=Yn?v)QY7jF*Ti`X24tkaDu@&;Ggkhvjd5dsC~` zuOr=f``GcHMR)F+1c) zS4Z!#cWz>_I5W36Mr-!b2TK}m&F)yykuhVsm|1sS!vCrKf}x@9oeak$9{Do7`S`GK z`y<^Xp1#Ku0{FCLUFI#Hb@=4UU50E~vv)8%xO}<2HZptj7sXmd?#BJe`YRLi{_Hc3 RJPPz3gQu&X%Q~loCIAhpfAjzV literal 0 HcmV?d00001 diff --git a/textures/256_dyes_palette_1.png b/textures/256_dyes_palette_1.png new file mode 100644 index 0000000000000000000000000000000000000000..b63f513b6304e2b4b1f0337a8f88b635aa440753 GIT binary patch literal 432 zcmV;h0Z;ykP)O{P!xdC6Zy$O5so539Mo@lPDkT$azpjxOvk}QbudvKip5Cg zyi)HZJMCWHh+B72+R37my|j}&2-C=fX=H*&^_rR+R{v5T5bY2g)VKdpAI{Vr^?m literal 0 HcmV?d00001 diff --git a/textures/256_dyes_palette_2.png b/textures/256_dyes_palette_2.png new file mode 100644 index 0000000000000000000000000000000000000000..1e39799dd1d78b0002b28478f65a05bc3da15c3a GIT binary patch literal 380 zcmV-?0fYXDP)vdE0t84%M0n0= z2js`9L`4;b=qks1x96OWO4@2fLk+$~6D=;<$d@%J`yCCaqKdk3C!dyxKmx8F_AJ|` z2yW>idN4#q6;-Zq4>H|E3z{g8Lk+csTkw;vq6SqA)X`Y928BP?LP9}IPfrs#KP+o8 zL@)dCp0?*Cs?#iM@qM;fv^igB6!tqBmojzX?tHSWe@jfeXxoVJEm_u=a^(v5a3;&T z=y9AaF5JRDx$a2=bz&@9qwvRi4b7akC&b6O?rDfF%6mUNJv}R7XIs{5XinOQmvf+U zXCG({ec?_#o#z(|{g&`_*|Y_bzKIcZG1k!a3