scls19fr / Nextion.jl

An unofficial Julia library to communicate with Itead Nextion display
Other
1 stars 1 forks source link

using a Color type package #8

Open scls19fr opened 6 years ago

scls19fr commented 6 years ago

Should I use a package for better handling of colors?

See how to integrate with Nextion Color Enums

scls19fr commented 6 years ago

https://github.com/JuliaGraphics/ColorTypes.jl https://github.com/JuliaGraphics/NamedColors.jl https://github.com/JuliaGraphics/Colors.jl https://github.com/JuliaMath/FixedPointNumbers.jl

scls19fr commented 6 years ago
julia> using ColorTypes

julia> using FixedPointNumbers

julia> import Base: UInt32

julia> r, g, b = 0xa0, 0x0c, 0x01
(0xa0, 0x0c, 0x01)

julia> r = reinterpret(N0f8, r)
0.627N0f8

julia> g = reinterpret(N0f8, g)
0.047N0f8

julia> b = reinterpret(N0f8, b)
0.004N0f8

julia> color = RGB(r, g, b)
RGB{N0f8}(0.627,0.047,0.004)

julia> # color = RGB{N0f8}(r/255, g/255, b/255)

julia> function UInt32(color::RGB{N0f8})
           UInt32(reinterpret(UInt8, color.r)) << 16 + UInt32(reinterpret(UInt8, color.g)) << 8 + UInt32(reinterpret(UInt8, color.b))
       end
UInt32

julia> value = UInt32(color)
0x00a00c01

julia> function RGB(value::UInt32)
           r = reinterpret(N0f8, UInt8((value & (UInt32(0xff) << 16)) >> 16))
           g = reinterpret(N0f8, UInt8((value & (UInt32(0xff) << 8)) >> 8))
           b = reinterpret(N0f8, UInt8(value & UInt32(0xff)))
           RGB(r, g, b)
       end
RGB

julia> RGB(color)
RGB{N0f8}(0.627,0.047,0.004)
scls19fr commented 6 years ago
julia> t = r, g, b = 0xa0, 0x0c, 0x01

julia> t = map(c->reinterpret(N0f8, c), t)

julia> color = RGB(t...)
scls19fr commented 6 years ago

Get RGB{N0f8} from Colors.color_names

RGB(map(c->reinterpret(N0f8, c), UInt8.(Colors.color_names["indianred"]))...)
scls19fr commented 6 years ago

see https://github.com/JuliaGraphics/Colors.jl/issues/324

scls19fr commented 6 years ago

Nextion colors

NONE = -1  # 0x
BLACK = UInt32(0)  # 0x00000000
WHITE = UInt32(65535)  # 0x0000ffff
RED = UInt32(63488)  # 0x0000f800
GREEN = UInt32(2016)  # 0x000007e0
BLUE = UInt32(31)  # 0x0000001f
GRAY = UInt32(33840)  # 0x00008430
BROWN = UInt32(48192)  # 0x0000bc40
YELLOW = UInt32(65504)  # 0x0000ffe0
scls19fr commented 5 years ago

https://en.wikipedia.org/wiki/High_color

16 bits colors:

Bits 0 to 4 give 32 values of blue, those of 5 to 10, 64 levels of green (the main element of luminance), and those 11 to 15 encode 32 values of red.

RRRRRGGGGGGBBBBB

scls19fr commented 5 years ago

Move https://github.com/scls19fr/Nextion.jl/blob/a305f757101a484d7611fc3cf3490599c358615b/src/constants.jl#L131-L150 to color.jl

See https://github.com/JuliaGraphics/Colors.jl/issues/332 Working with 2 bytes (16 bits) colors and https://github.com/JuliaGraphics/ColorTypes.jl/issues/114 Implement RGB16 / RGB565 and https://github.com/JuliaGraphics/ColorTypes.jl/issues/115 Implement RGB8 / RGB332

scls19fr commented 5 years ago

ToFix

import Base: UInt16

r, g, b = UInt8.((16, 33, 16))  # gray
r = reinterpret(Normed{UInt8,5}, r)
g = reinterpret(Normed{UInt8,6}, g)
b = reinterpret(Normed{UInt8,5}, b)
color = RGB(r, g, b)
function UInt16(color)
    UInt16(reinterpret(UInt8, color.r)) << 11 + UInt16(reinterpret(UInt8, color.g)) << 5 + UInt16(reinterpret(UInt8, color.b))
end
scls19fr commented 5 years ago
using ColorTypes
using FixedPointNumbers
import Base: UInt16

"""
`RGB16` is Red-Green-Blue colorspace represented by two bytes (16 bits).

1        |1          |        0
5 4 3 2 1|0 9 8 7 6 5|4 3 2 1 0 
---------|-----------|---------
R R R R R|G G G G G G|B B B B B
"""
struct RGB16
    value::UInt16
end

function RGB16(r, g, b)
    RGB16(r << 11 + g << 5 + b)
end

red(c::RGB16) = (c.value & 0xf800) >> 11
green(c::RGB16) = (c.value & 0x07e0) >> 5
blue(c::RGB16) = c.value & 0x001f

# red(c::RGB16) = reinterpret(N3f5, (c.value & 0xf800) >> 11)
# green(c::RGB16) = reinterpret(N2f6, (c.value & 0x07e0) >> 5)
# blue(c::RGB16) = reinterpret(N3f5, c.value & 0x001f)

# reinterpret(::Type{UInt16}, c::RGB16) = c.value
UInt16(c::RGB16) = c.value

using Test
@testset "RGB16" begin
  c = RGB16(31, 63, 31)  # white
  @test blue(c) == 31
  @test green(c) == 63
  @test red(c) == 31
  @test UInt16(c) == 65535

  c = RGB16(0, 0, 0)  # black
  @test UInt16(c) == 0

  c = RGB16(31, 0, 0)  # red
  @test UInt16(c) == 63488

   c = RGB16(0, 63, 0)  # green
  @test UInt16(c) == 2016

   c = RGB16(0, 0, 31)  # blue
  @test UInt16(c) == 31

  c = RGB16(16, 33, 16)  # gray
  @test UInt16(c) == 33840

  c = RGB16(23, 34, 0)  # brown
  @test UInt16(c) == 48192

  c = RGB16(31, 63, 0)  # yellow
  @test UInt16(c) == 65504
end