Make a LCHab{Float32} color darker

I’ll be direct! I’m trying to make a LCHab{Float32} color made with Colors.jl darker; at first glance, I thought in iterating and giving it a smaller value, however perhaps I’m very dumb for not knowing that I cannot probably iterate it, Julia 1.5.3 gave the following error trying with foreach and println:

julia> w = LCHab{Float32}(70.0f0,70.0f0,0.0f0)
LCHab{Float32}(70.0f0,70.0f0,0.0f0)

julia> foreach(println, w)
ERROR: MethodError: no method matching iterate(::LCHab{Float32})
Closest candidates are:
  iterate(::Base.MethodList, ::Any...) at reflection.jl:874
  iterate(::LibGit2.GitBranchIter) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/LibGit2/src/reference.jl:343
  iterate(::LibGit2.GitBranchIter, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/LibGit2/src/reference.jl:343
  ...
Stacktrace:
 [1] foreach(::typeof(println), ::LCHab{Float32}) at ./abstractarray.jl:2009
 [2] top-level scope at REPL[13]:1
 [3] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288

julia> w[1]
ERROR: MethodError: no method matching getindex(::LCHab{Float32}, ::Int64)
Stacktrace:
 [1] top-level scope at REPL[14]:1
 [2] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288

Or perhaps it is iterable and I just can’t figure it out, anyway, I’d be glad if someone can show me how to make this type of color darker.

It’s a struct so you need to access each of the fields:

julia> w = LCHab{Float32}(70.0f0,70.0f0,0.0f0)
LCHab{Float32}(70.0f0,70.0f0,0.0f0)

julia> dump(w)
LCHab{Float32}
  l: Float32 70.0f0
  c: Float32 70.0f0
  h: Float32 0.0f0

julia> foreach(x->println(getfield(w,x)), fieldnames(typeof(w)))
70.0
70.0
0.0

it’s not mutable, so you need to make a new LCHab that only has difference in the field l? (which controls Luminance?)

julia> b = LCHab(w.l * 0.8f0, w.c, w.h)
LCHab{Float32}(56.0f0,70.0f0,0.0f0)
4 Likes

Thank you, it worked as expected!