From 6015a255d63b2fba1c2382168a172fea6bd5cb0c Mon Sep 17 00:00:00 2001 From: Alex Clink Date: Tue, 29 Mar 2022 22:54:31 -0400 Subject: [PATCH] Change colors to constants --- examples/balls.cr | 2 +- examples/noise_1d.cr | 2 +- examples/piano.cr | 4 +- examples/procedural.cr | 4 +- examples/snow.cr | 2 +- examples/triangle.cr | 2 +- src/3d/tri.cr | 4 +- src/colors.cr | 151 ++++++++++++++++++++++++++++++++++++++ src/pixel.cr | 66 +++++++++-------- src/sprite/draw_string.cr | 2 +- 10 files changed, 196 insertions(+), 43 deletions(-) create mode 100644 src/colors.cr diff --git a/examples/balls.cr b/examples/balls.cr index ea4eff6..537d3eb 100644 --- a/examples/balls.cr +++ b/examples/balls.cr @@ -72,7 +72,7 @@ module PF fill_shape(Shape.translate(ball.frame, translation: ball.position).map(&.to_i32), ball.color) # draw_circle(ball.position.to_i32, ball.radius.to_i32, Pixel.green) end - draw_string("Balls: #{@balls.size}", 5, 5, Pixel.white) + draw_string("Balls: #{@balls.size}", 5, 5, Pixel::White) end end end diff --git a/examples/noise_1d.cr b/examples/noise_1d.cr index c31b5df..ebb5b9f 100644 --- a/examples/noise_1d.cr +++ b/examples/noise_1d.cr @@ -39,7 +39,7 @@ module PF 0.upto(width) do |x| y = mid + (@noise.get((x + @xpos) / @noise_zoom) * @noise_scale).to_i - draw_point(x, y, Pixel.yellow) + draw_point(x, y, Pixel::Yellow) end end end diff --git a/examples/piano.cr b/examples/piano.cr index cd37d3e..0631362 100644 --- a/examples/piano.cr +++ b/examples/piano.cr @@ -153,14 +153,14 @@ module PF @white_keys.each do |key| top_left, bottom_right, name = key - fill_rect(top_left, bottom_right, @keysdown[name]? ? @highlight : Pixel.white) + fill_rect(top_left, bottom_right, @keysdown[name]? ? @highlight : Pixel::White) draw_rect(top_left, bottom_right, Pixel.new(127, 127, 127)) draw_string(name, top_left.x + 2, top_left.y + (@key_size * 2) - Sprite::CHAR_HEIGHT - 2, @keysdown[name]? ? @text_hl : @text_color) end @black_keys.each do |key| top_left, bottom_right, name = key - fill_rect(top_left, bottom_right, @keysdown[name]? ? @highlight : Pixel.black) + fill_rect(top_left, bottom_right, @keysdown[name]? ? @highlight : Pixel::Black) draw_rect(top_left, bottom_right, Pixel.new(127, 127, 127)) draw_string(name, top_left.x + 2, top_left.y + @key_size - Sprite::CHAR_HEIGHT - 2, @keysdown[name]? ? @text_hl : @text_color) end diff --git a/examples/procedural.cr b/examples/procedural.cr index c3c1c78..c977ef7 100644 --- a/examples/procedural.cr +++ b/examples/procedural.cr @@ -54,12 +54,12 @@ module PF b = @random.rand(0u8..0xFFu8) (@buffer + n).value = Pixel.new(b, b, b).to_u32 else - (@buffer + n).value = Pixel.black.to_u32 + (@buffer + n).value = Pixel::Black.to_u32 end end end time = elapsed_milliseconds - start - draw_string("frame: #{time.round(2)}ms", 5, 5, Pixel.white, bg: Pixel.black) + draw_string("frame: #{time.round(2)}ms", 5, 5, Pixel::White, bg: Pixel::Black) end end end diff --git a/examples/snow.cr b/examples/snow.cr index 676838c..31a4a8f 100644 --- a/examples/snow.cr +++ b/examples/snow.cr @@ -97,7 +97,7 @@ class Snow < PF::Game clear(0, 0, 15) @flakes.each do |flake| - color = PF::Pixel.white * flake.z_pos + color = PF::Pixel::White * flake.z_pos if flake.shape == 0 draw_point(flake.position.to_i32, color) else diff --git a/examples/triangle.cr b/examples/triangle.cr index b720c04..f04951f 100644 --- a/examples/triangle.cr +++ b/examples/triangle.cr @@ -18,7 +18,7 @@ class Triangle < PF::Entity def draw(engine) _frame = PF::Shape.rotate(@frame, @rotation) _frame = PF::Shape.translate(_frame, @position) - engine.fill_triangle(_frame.map(&.to_i32), PF::Pixel.yellow) + engine.fill_triangle(_frame.map(&.to_i32), PF::Pixel::Yellow) end end diff --git a/src/3d/tri.cr b/src/3d/tri.cr index d1c12da..0658416 100644 --- a/src/3d/tri.cr +++ b/src/3d/tri.cr @@ -16,10 +16,10 @@ module PF setter normal : Vector3(Float64)? - def initialize(@p1 : Vector3(Float64), @p2 : Vector3(Float64), @p3 : Vector3(Float64), @color = PF::Pixel.white, @normal = nil) + def initialize(@p1 : Vector3(Float64), @p2 : Vector3(Float64), @p3 : Vector3(Float64), @color = PF::Pixel::White, @normal = nil) end - def initialize(@p1, @p2, @p3, @t1, @t2, @t3, @color = PF::Pixel.white) + def initialize(@p1, @p2, @p3, @t1, @t2, @t3, @color = PF::Pixel::White) end # Return the normal assuming clockwise pointing winding diff --git a/src/colors.cr b/src/colors.cr new file mode 100644 index 0000000..0f613cb --- /dev/null +++ b/src/colors.cr @@ -0,0 +1,151 @@ +module PF + struct Pixel + AliceBlue = Pixel.new(0xF0_F8_FF_FF_u32) + AntiqueWhite = Pixel.new(0xFA_EB_D7_FF_u32) + Aqua = Pixel.new(0x00_FF_FF_FF_u32) + Aquamarine = Pixel.new(0x7F_FF_D4_FF_u32) + Azure = Pixel.new(0xF0_FF_FF_FF_u32) + Beige = Pixel.new(0xF5_F5_DC_FF_u32) + Bisque = Pixel.new(0xFF_E4_C4_FF_u32) + Black = Pixel.new(0x00_00_00_FF_u32) + BlanchedAlmond = Pixel.new(0xFF_EB_CD_FF_u32) + Blue = Pixel.new(0x00_00_FF_FF_u32) + BlueViolet = Pixel.new(0x8A_2B_E2_FF_u32) + Brown = Pixel.new(0xA5_2A_2A_FF_u32) + BurlyWood = Pixel.new(0xDE_B8_87_FF_u32) + CadetBlue = Pixel.new(0x5F_9E_A0_FF_u32) + Chartreuse = Pixel.new(0x7F_FF_00_FF_u32) + Chocolate = Pixel.new(0xD2_69_1E_FF_u32) + Coral = Pixel.new(0xFF_7F_50_FF_u32) + CornflowerBlue = Pixel.new(0x64_95_ED_FF_u32) + Cornsilk = Pixel.new(0xFF_F8_DC_FF_u32) + Crimson = Pixel.new(0xDC_14_3C_FF_u32) + Cyan = Pixel.new(0x00_FF_FF_FF_u32) + DarkBlue = Pixel.new(0x00_00_8B_FF_u32) + DarkCyan = Pixel.new(0x00_8B_8B_FF_u32) + DarkGoldenRod = Pixel.new(0xB8_86_0B_FF_u32) + DarkGray = Pixel.new(0xA9_A9_A9_FF_u32) + DarkGrey = Pixel.new(0xA9_A9_A9_FF_u32) + DarkGreen = Pixel.new(0x00_64_00_FF_u32) + DarkKhaki = Pixel.new(0xBD_B7_6B_FF_u32) + DarkMagenta = Pixel.new(0x8B_00_8B_FF_u32) + DarkOliveGreen = Pixel.new(0x55_6B_2F_FF_u32) + Darkorange = Pixel.new(0xFF_8C_00_FF_u32) + DarkOrchid = Pixel.new(0x99_32_CC_FF_u32) + DarkRed = Pixel.new(0x8B_00_00_FF_u32) + DarkSalmon = Pixel.new(0xE9_96_7A_FF_u32) + DarkSeaGreen = Pixel.new(0x8F_BC_8F_FF_u32) + DarkSlateBlue = Pixel.new(0x48_3D_8B_FF_u32) + DarkSlateGray = Pixel.new(0x2F_4F_4F_FF_u32) + DarkSlateGrey = Pixel.new(0x2F_4F_4F_FF_u32) + DarkTurquoise = Pixel.new(0x00_CE_D1_FF_u32) + DarkViolet = Pixel.new(0x94_00_D3_FF_u32) + DeepPink = Pixel.new(0xFF_14_93_FF_u32) + DeepSkyBlue = Pixel.new(0x00_BF_FF_FF_u32) + DimGray = Pixel.new(0x69_69_69_FF_u32) + DimGrey = Pixel.new(0x69_69_69_FF_u32) + DodgerBlue = Pixel.new(0x1E_90_FF_FF_u32) + FireBrick = Pixel.new(0xB2_22_22_FF_u32) + FloralWhite = Pixel.new(0xFF_FA_F0_FF_u32) + ForestGreen = Pixel.new(0x22_8B_22_FF_u32) + Fuchsia = Pixel.new(0xFF_00_FF_FF_u32) + Gainsboro = Pixel.new(0xDC_DC_DC_FF_u32) + GhostWhite = Pixel.new(0xF8_F8_FF_FF_u32) + Gold = Pixel.new(0xFF_D7_00_FF_u32) + GoldenRod = Pixel.new(0xDA_A5_20_FF_u32) + Gray = Pixel.new(0x80_80_80_FF_u32) + Grey = Pixel.new(0x80_80_80_FF_u32) + Green = Pixel.new(0x00_80_00_FF_u32) + GreenYellow = Pixel.new(0xAD_FF_2F_FF_u32) + HoneyDew = Pixel.new(0xF0_FF_F0_FF_u32) + HotPink = Pixel.new(0xFF_69_B4_FF_u32) + IndianRed = Pixel.new(0xCD_5C_5C_FF_u32) + Indigo = Pixel.new(0x4B_00_82_FF_u32) + Ivory = Pixel.new(0xFF_FF_F0_FF_u32) + Khaki = Pixel.new(0xF0_E6_8C_FF_u32) + Lavender = Pixel.new(0xE6_E6_FA_FF_u32) + LavenderBlush = Pixel.new(0xFF_F0_F5_FF_u32) + LawnGreen = Pixel.new(0x7C_FC_00_FF_u32) + LemonChiffon = Pixel.new(0xFF_FA_CD_FF_u32) + LightBlue = Pixel.new(0xAD_D8_E6_FF_u32) + LightCoral = Pixel.new(0xF0_80_80_FF_u32) + LightCyan = Pixel.new(0xE0_FF_FF_FF_u32) + LightGoldenRodYellow = Pixel.new(0xFA_FA_D2_FF_u32) + LightGray = Pixel.new(0xD3_D3_D3_FF_u32) + LightGrey = Pixel.new(0xD3_D3_D3_FF_u32) + LightGreen = Pixel.new(0x90_EE_90_FF_u32) + LightPink = Pixel.new(0xFF_B6_C1_FF_u32) + LightSalmon = Pixel.new(0xFF_A0_7A_FF_u32) + LightSeaGreen = Pixel.new(0x20_B2_AA_FF_u32) + LightSkyBlue = Pixel.new(0x87_CE_FA_FF_u32) + LightSlateGray = Pixel.new(0x77_88_99_FF_u32) + LightSlateGrey = Pixel.new(0x77_88_99_FF_u32) + LightSteelBlue = Pixel.new(0xB0_C4_DE_FF_u32) + LightYellow = Pixel.new(0xFF_FF_E0_FF_u32) + Lime = Pixel.new(0x00_FF_00_FF_u32) + LimeGreen = Pixel.new(0x32_CD_32_FF_u32) + Linen = Pixel.new(0xFA_F0_E6_FF_u32) + Magenta = Pixel.new(0xFF_00_FF_FF_u32) + Maroon = Pixel.new(0x80_00_00_FF_u32) + MediumAquaMarine = Pixel.new(0x66_CD_AA_FF_u32) + MediumBlue = Pixel.new(0x00_00_CD_FF_u32) + MediumOrchid = Pixel.new(0xBA_55_D3_FF_u32) + MediumPurple = Pixel.new(0x93_70_D8_FF_u32) + MediumSeaGreen = Pixel.new(0x3C_B3_71_FF_u32) + MediumSlateBlue = Pixel.new(0x7B_68_EE_FF_u32) + MediumSpringGreen = Pixel.new(0x00_FA_9A_FF_u32) + MediumTurquoise = Pixel.new(0x48_D1_CC_FF_u32) + MediumVioletRed = Pixel.new(0xC7_15_85_FF_u32) + MidnightBlue = Pixel.new(0x19_19_70_FF_u32) + MintCream = Pixel.new(0xF5_FF_FA_FF_u32) + MistyRose = Pixel.new(0xFF_E4_E1_FF_u32) + Moccasin = Pixel.new(0xFF_E4_B5_FF_u32) + NavajoWhite = Pixel.new(0xFF_DE_AD_FF_u32) + Navy = Pixel.new(0x00_00_80_FF_u32) + OldLace = Pixel.new(0xFD_F5_E6_FF_u32) + Olive = Pixel.new(0x80_80_00_FF_u32) + OliveDrab = Pixel.new(0x6B_8E_23_FF_u32) + Orange = Pixel.new(0xFF_A5_00_FF_u32) + OrangeRed = Pixel.new(0xFF_45_00_FF_u32) + Orchid = Pixel.new(0xDA_70_D6_FF_u32) + PaleGoldenRod = Pixel.new(0xEE_E8_AA_FF_u32) + PaleGreen = Pixel.new(0x98_FB_98_FF_u32) + PaleTurquoise = Pixel.new(0xAF_EE_EE_FF_u32) + PaleVioletRed = Pixel.new(0xD8_70_93_FF_u32) + PapayaWhip = Pixel.new(0xFF_EF_D5_FF_u32) + PeachPuff = Pixel.new(0xFF_DA_B9_FF_u32) + Peru = Pixel.new(0xCD_85_3F_FF_u32) + Pink = Pixel.new(0xFF_C0_CB_FF_u32) + Plum = Pixel.new(0xDD_A0_DD_FF_u32) + PowderBlue = Pixel.new(0xB0_E0_E6_FF_u32) + Purple = Pixel.new(0x80_00_80_FF_u32) + Red = Pixel.new(0xFF_00_00_FF_u32) + RosyBrown = Pixel.new(0xBC_8F_8F_FF_u32) + RoyalBlue = Pixel.new(0x41_69_E1_FF_u32) + SaddleBrown = Pixel.new(0x8B_45_13_FF_u32) + Salmon = Pixel.new(0xFA_80_72_FF_u32) + SandyBrown = Pixel.new(0xF4_A4_60_FF_u32) + SeaGreen = Pixel.new(0x2E_8B_57_FF_u32) + SeaShell = Pixel.new(0xFF_F5_EE_FF_u32) + Sienna = Pixel.new(0xA0_52_2D_FF_u32) + Silver = Pixel.new(0xC0_C0_C0_FF_u32) + SkyBlue = Pixel.new(0x87_CE_EB_FF_u32) + SlateBlue = Pixel.new(0x6A_5A_CD_FF_u32) + SlateGray = Pixel.new(0x70_80_90_FF_u32) + SlateGrey = Pixel.new(0x70_80_90_FF_u32) + Snow = Pixel.new(0xFF_FA_FA_FF_u32) + SpringGreen = Pixel.new(0x00_FF_7F_FF_u32) + SteelBlue = Pixel.new(0x46_82_B4_FF_u32) + Tan = Pixel.new(0xD2_B4_8C_FF_u32) + Teal = Pixel.new(0x00_80_80_FF_u32) + Thistle = Pixel.new(0xD8_BF_D8_FF_u32) + Tomato = Pixel.new(0xFF_63_47_FF_u32) + Turquoise = Pixel.new(0x40_E0_D0_FF_u32) + Violet = Pixel.new(0xEE_82_EE_FF_u32) + Wheat = Pixel.new(0xF5_DE_B3_FF_u32) + White = Pixel.new(0xFF_FF_FF_FF_u32) + WhiteSmoke = Pixel.new(0xF5_F5_F5_FF_u32) + Yellow = Pixel.new(0xFF_FF_00_FF_u32) + YellowGreen = Pixel.new(0x9A_CD_32_FF_u32) + end +end diff --git a/src/pixel.cr b/src/pixel.cr index f74bc24..45b9e7e 100644 --- a/src/pixel.cr +++ b/src/pixel.cr @@ -1,41 +1,11 @@ +require "./colors" + module PF struct Pixel def self.random new(rand(0_u8..0xFF_u8), rand(0_u8..0xFF_u8), rand(0_u8..0xFF_u8), 0xFF_u8) end - def self.white - new(255, 255, 255) - end - - def self.black - new(0, 0, 0) - end - - def self.red - new(255, 0, 0) - end - - def self.green - new(0, 255, 0) - end - - def self.blue - new(0, 0, 255) - end - - def self.yellow - new(255, 255, 0) - end - - def self.magenta - new(255, 0, 255) - end - - def self.cyan - new(0, 255, 255) - end - property r : UInt8, g : UInt8, b : UInt8, a : UInt8 def initialize(rgba : UInt32) @@ -52,6 +22,38 @@ module PF LibSDL.map_rgba(format, @r, @g, @b, @a) end + def blend_value(v1 : UInt8, v2 : UInt8, t : Float64) : UInt8 + f1 = v1 / UInt8::MAX + f2 = v2 / UInt8::MAX + v = Math.sqrt((1 - t) * f1 ** 2 + t * f2 ** 2) + (v * UInt8::MAX).to_u8 + end + + def blend(other : Pixel, t : Float64 = 0.5) + Pixel.new( + blend_value(@r, other.r, t), + blend_value(@g, other.g, t), + blend_value(@b, other.b, t), + @a + ) + end + + def add(other : Pixel) + Pixel.new( + ((@r.to_u16 + other.r) // 2).to_u8, + ((@g.to_u16 + other.g) // 2).to_u8, + ((@b.to_u16 + other.b) // 2).to_u8 + ) + end + + def darken(other : Pixel) + Pixel.new( + (@r * (other.r / 255)).to_u8, + (@g * (other.g / 255)).to_u8, + (@b * (other.b / 255)).to_u8 + ) + end + def *(n : Float64) Pixel.new((@r * n).to_u8, (@g * n).to_u8, (@b * n).to_u8, @a) end diff --git a/src/sprite/draw_string.cr b/src/sprite/draw_string.cr index c252a0c..2d5f34d 100644 --- a/src/sprite/draw_string.cr +++ b/src/sprite/draw_string.cr @@ -124,7 +124,7 @@ module PF end end - def draw_string(msg : String, pos : Vector2(Int), color : Pixel = Pixel.black) + def draw_string(msg : String, pos : Vector2(Int), color : Pixel = Pixel::Black) draw_string(msg, pos.x, pos.y, color) end end