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

randomized colors for entities based on id hash #44

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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