Skip to content

Commit

Permalink
Merge ec3f356 into 3aebbb8
Browse files Browse the repository at this point in the history
  • Loading branch information
kmsquire committed Sep 17, 2014
2 parents 3aebbb8 + ec3f356 commit 256fcf9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/colorspaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,5 @@ argb32{T}(c::ColorValue{T}) = ARGB32(convert(RGB24,c).color | 0xff000000)

const CVconcrete = (HSV, HSL, XYZ, xyY, Lab, Luv, LCHab, LCHuv, DIN99, DIN99d, DIN99o, LMS)
const CVparametric = tuple(RGB, CVconcrete...)
const CVfractional = (XYZ,)
const CVfloatingpoint = (HSV, HSL, xyY, Lab, Luv, LCHab, LCHuv, DIN99, DIN99d, DIN99o, LMS)
37 changes: 22 additions & 15 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ end


# Conversions where the datatype is not specified
for CV in CVconcrete
@eval begin
# preserves the datatype of the original space
convert{T<:Fractional}(::Type{$CV}, c::ColorValue{T}) = convert($CV{T}, c)
# fallback is Float64 (needed for RGB24)
convert(::Type{$CV}, c) = convert($CV{Float64}, c)
for (FloatClass, FloatCollection) in [(Fractional, CVfractional), (FloatingPoint, CVfloatingpoint)]
for CV in FloatCollection
@eval begin
# preserves the datatype of the original space
convert{T<:$FloatClass}(::Type{$CV}, c::ColorValue{T}) = convert($CV{T}, c)
# fallback is Float64 (needed for RGB24)
convert(::Type{$CV}, c) = convert($CV{Float64}, c)
end
end
end
convert{T}(::Type{RGB}, c::ColorValue{T}) = _convert(RGB{T}, c)
Expand Down Expand Up @@ -100,15 +102,15 @@ function _convert{CV<:AbstractRGB}(::Type{CV}, c::XYZ)
srgb_compand(ans[3])))
end

_convert{CV<:AbstractRGB}(::Type{CV}, c::xyY) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::Lab) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::LCHab) = _convert(CV, convert(Lab{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::Luv) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::LCHuv) = _convert(CV, convert(Luv{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::DIN99) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::DIN99o) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::DIN99d) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::LMS) = _convert(CV, convert(XYZ{eltype(CV)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::xyY) = _convert(CV, convert(XYZ{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::Lab) = _convert(CV, convert(XYZ{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::LCHab) = _convert(CV, convert(Lab{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::Luv) = _convert(CV, convert(XYZ{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::LCHuv) = _convert(CV, convert(Luv{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::DIN99) = _convert(CV, convert(XYZ{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::DIN99o) = _convert(CV, convert(XYZ{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::DIN99d) = _convert(CV, convert(XYZ{eltype(c)}, c))
_convert{CV<:AbstractRGB}(::Type{CV}, c::LMS) = _convert(CV, convert(XYZ{eltype(c)}, c))

_convert{CV<:AbstractRGB}(::Type{CV}, c::RGB24) = CV((c.color&0x00ff0000>>>16)/255, ((c.color&0x0000ff00)>>>8)/255, (c.color&0x000000ff)/255)

Expand Down Expand Up @@ -244,6 +246,10 @@ convert{T}(::Type{XYZ{T}}, c::LCHab) = convert(XYZ{T}, convert(Lab{T}, c))
convert{T}(::Type{XYZ{T}}, c::DIN99) = convert(XYZ{T}, convert(Lab{T}, c))
convert{T}(::Type{XYZ{T}}, c::DIN99o) = convert(XYZ{T}, convert(Lab{T}, c))

convert{T<:Ufixed}(::Type{XYZ{T}}, c::LCHab) = convert(XYZ{T}, convert(Lab{Float64}, c))
convert{T<:Ufixed}(::Type{XYZ{T}}, c::DIN99) = convert(XYZ{T}, convert(Lab{Float64}, c))
convert{T<:Ufixed}(::Type{XYZ{T}}, c::DIN99o) = convert(XYZ{T}, convert(Lab{Float64}, c))


function xyz_to_uv(c::XYZ)
d = c.x + 15c.y + 3c.z
Expand All @@ -269,6 +275,7 @@ end

convert{T}(::Type{XYZ{T}}, c::Luv) = convert(XYZ{T}, c, WP_DEFAULT)
convert{T}(::Type{XYZ{T}}, c::LCHuv) = convert(XYZ{T}, convert(Luv{T}, c))
convert{T<:Ufixed}(::Type{XYZ{T}}, c::LCHuv) = convert(XYZ{T}, convert(Luv{Float64}, c))


function convert{T}(::Type{XYZ{T}}, c::DIN99d)
Expand Down
30 changes: 30 additions & 0 deletions test/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ for Cto in Color.CVparametric
@test typeof(convert(Cto{Float32}, red24)) == Cto{Float32}
end

# Test conversion from Ufixed types
for Cto in Color.CVfloatingpoint
for Cfrom in (RGB, XYZ)
for Tto in (Float32, Float64)
for Tfrom in (Ufixed8, Ufixed10, Ufixed12, Ufixed14, Ufixed16)
c = convert(Cfrom{Tfrom}, red)
@test typeof(c) == Cfrom{Tfrom}
c1 = convert(Cto, c)
@test eltype(c1) == Float64
c2 = convert(Cto{Tto}, c)
@test typeof(c2) == Cto{Tto}
end
end
end
end

# Test conversion to Ufixed types
for Cto in (RGB, XYZ)
for Cfrom in Color.CVfloatingpoint
for Tto in (Ufixed8, Ufixed10, Ufixed12, Ufixed14, Ufixed16)
for Tfrom in (Float32, Float64)
c = convert(Cfrom{Tfrom}, red)
@test typeof(c) == Cfrom{Tfrom}
c2 = convert(Cto{Tto}, c)
@test typeof(c2) == Cto{Tto}
end
end
end
end

ac = rgba(red)
@test convert(ARGB32, ac) == ARGB32(0xffff0000)
@test convert(Uint32, convert(ARGB32, ac)) == 0xffff0000
Expand Down

0 comments on commit 256fcf9

Please sign in to comment.