如何在Julia语言中将RGB值转换为字符串十六进制三元组

时间:2018-03-05 20:26:22

标签: julia

我正在使用colors.jl包并且具有我需要转换为十六进制字符串三元组值的RGB值。最好的方法是什么?找不到colors.jl

中的转换函数

感谢

2 个答案:

答案 0 :(得分:2)

您可以定义一个自定义struct,该AbstractRGB可以从show构建,并且具有适当的import Base: show using ColorTypes struct RGBPacked r::UInt8 g::UInt8 b::UInt8 function (::Type{RGBPacked})(c::AbstractRGB) colvec = (red(c), green(c), blue(c)) temp = map(UInt8, map(x->floor(255*x), colvec)) new(temp[1], temp[2], temp[3]) end end function show(io::IO, r::RGBPacked) print(io, "#$(hex(r.r))$(hex(r.g))$(hex(r.b))") end 重载,如

c1 = RGB(1.,0.5,0.7)
c2 = RGBPacked(c1)

然后,您可以使用

中的自定义数据类型
Main> c1 = RGB(1.,0.5,0.7)
RGB{Float64}(1.0,0.5,0.7)

Main> c2 = RGBPacked(c1)
#ff7fb2

打印

public class CustomItem {

    private final String text;

    public CustomItem(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

}

答案 1 :(得分:1)

根据 Bogumil 的建议,Colors.jl 提供了 hex 函数:

julia> using Colors

julia> hex(RGB(1,0.5,0))
"FF8000"