Skip to content

Commit

Permalink
Merge pull request #44 from mattuntergassmair/v0.8_random_color_hash
Browse files Browse the repository at this point in the history
randomized colors for entities based on id hash
  • Loading branch information
mattuntergassmair committed Mar 3, 2020
2 parents 6125e87 + 36d4056 commit 716a1c8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/renderable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,18 @@ Helper function for directly rendering entities, takes care of wrapping them in
function add_renderable!(
rendermodel::RenderModel,
entity::E,
color::Colorant=RGB(rand(), rand(), rand())
color::Union{Nothing, Colorant}=nothing
) where {E<:Entity}
if color === nothing
# random color based on hash code of entity.id
# see https://stackoverflow.com/questions/11120840/hash-string-into-rgb-color
idhash = hash(entity.id)
color = RGB(
.3 + .7*((idhash & 0xFF0000) >> 16)/255,
.3 + .7*((idhash & 0x00FF00) >> 8)/255,
.3 + .7*((idhash & 0x0000FF))/255,
)
end
if rendermode == :fancy
fe = (class(entity.def) == AgentClass.PEDESTRIAN ? FancyPedestrian(ped=entity, color=color) : FancyCar(car=entity, color=color))
add_renderable!(rendermodel, fe)
Expand Down

0 comments on commit 716a1c8

Please sign in to comment.