Skip to content

Instantly share code, notes, and snippets.

@nineonefive
Last active October 1, 2020 19:42
Show Gist options
  • Save nineonefive/801a957a6cee26ed9870eff1f8a45f3d to your computer and use it in GitHub Desktop.
Save nineonefive/801a957a6cee26ed9870eff1f8a45f3d to your computer and use it in GitHub Desktop.
Script that can visualize how images (like banners or sprays) would look in Minecraft 1.8
using Colors
using Images
using ProgressMeter
using Distributed
# 1.8 base colors, see https://minecraft.gamepedia.com/Map_item_format#Map_colors
baseColors = [0 0 0;
127 178 56;
247 233 163;
167 167 167;
255 0 0;
160 160 255;
167 167 167;
0 124 0;
255 255 255;
164 168 184;
183 106 47;
112 112 112;
64 64 255;
104 83 50;
255 252 245;
216 127 51;
178 76 216;
102 153 216;
229 229 51;
127 204 25;
242 127 165;
76 76 76;
153 153 153;
76 127 153;
127 63 178;
51 76 178;
102 76 51;
102 127 51;
153 51 51;
25 25 25;
250 238 77;
92 219 213;
74 128 255;
0 217 58;
21 20 31;
112 2 0;
126 84 48]
# compute the variants
mapColors = map(eachrow(baseColors)) do color
[RGB((floor.(color .* mult) ./ 255)...) for mult in [255 180 220 135] ./ 255]
end
# restructure so that each element of the array is an RGB element corresponding to a color variant
mapColors = hcat(mapColors...)[1, :]
"""
closestPixel(pixel::RGB; metric=DE_2000())
Maps the pixel (given in RGB from Colors.jl) to the closest permitted color in Minecraft 1.8 using the Delta-E color metric. Optionally
takes another color metric to use. Returns a color from `mapColors`.
"""
function closestPixel(pixel::RGB; m=DE_2000())
Δ = colordiff.(pixel, mapColors; metric=m)
Δ₀ = mapColors[argmin(Δ)]
end
"""
mcConvert(img; metric=DE_2000())
Converts an image using as many available processes as possible (can do distributed computing with the Distributed library).
# Example
Load an image and convert it with 4 processes
```julia-repl
julia> using Distributed
julia> addprocs(4) # or start julia with 'julia -p 4'
julia> @everywhere include("BannerDisplay.jl") # imports this file in all the processes
julia> img = load("test.png")
julia> R = mcConvert(img)
# then can show images with Plots.jl or save them
```
"""
function mcConvert(img; metric=DE_2000())
img = RGB.(img)
# Distributed computing, shows progressbar and number of processes.
println("Starting with $(nworkers()) processes...")
R = @showprogress pmap(eachcol(img)) do px # column-wise operations are faster in Julia
rgb = closestPixel.(px)
end
return hcat(R...) # convert our array of arrays to a single matrix of pixels
end
@nineonefive
Copy link
Author

Original image (1920x1080):
Color Booster
In MC 1.8:
Color Booster - MC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment