diff --git a/colors.v b/colors.v index ab8b3413..970006e0 100644 --- a/colors.v +++ b/colors.v @@ -41,3 +41,13 @@ pub const status_dark_lilac = tea.Color{ 154, 119, 209 } pub const status_cyan = tea.Color{ 138, 222, 237 } pub const status_purple = tea.Color{ 130, 144, 250 } +pub fn fg_color(background_color tea.Color) tea.Color { + s_r := f32(background_color.r) / 255 + s_g := f32(background_color.g) / 255 + s_b := f32(background_color.b) / 255 + + luminance := 0.2126 * s_r + 0.7152 * s_g + 0.0722 * s_b + + return if luminance > 0.5 { matte_black_fg_color } else { matte_white_fg_color } +} + diff --git a/colors_test.v b/colors_test.v new file mode 100644 index 00000000..1b93179a --- /dev/null +++ b/colors_test.v @@ -0,0 +1,9 @@ +module palette + +import tauraamui.bobatea as tea + +fn test_fg_color_from_shades_of_bg() { + assert fg_color(tea.Color{ 10, 10, 10 }) == matte_white_fg_color + assert fg_color(tea.Color{ 200, 200, 200 }) == matte_black_fg_color +} + diff --git a/editor.v b/editor.v index 83895cdf..608c2ac5 100644 --- a/editor.v +++ b/editor.v @@ -194,7 +194,8 @@ fn (m EditorModel) view(mut ctx tea.Context) { } fn (m EditorModel) render_cursor(mut ctx tea.Context) { - ctx.set_bg_color(palette.matte_white_fg_color) + default_bg_color := ctx.get_default_bg_color() or { palette.matte_black_bg_color } + ctx.set_bg_color(palette.fg_color(default_bg_color)) ctx.draw_rect(m.cursor_pos.x, m.cursor_pos.y, 1, 1) ctx.reset_bg_color() } diff --git a/editor_workspace.v b/editor_workspace.v index 586d627f..397009af 100644 --- a/editor_workspace.v +++ b/editor_workspace.v @@ -548,7 +548,7 @@ fn (m EditorWorkspaceModel) render_status_blocks(mut ctx tea.Context) { ctx.push_offset(tea.Offset{ x: 2 }) file_name_label := m.active_file_name() - ctx.set_color(palette.matte_white_fg_color) + ctx.set_color(palette.fg_color(palette.status_file_name_bg_color)) ctx.set_bg_color(palette.status_file_name_bg_color) ctx.draw_text(0, 0, file_name_label) ctx.reset_bg_color() diff --git a/main.v b/main.v index d4db8707..5e50dd90 100644 --- a/main.v +++ b/main.v @@ -4,7 +4,7 @@ import tauraamui.bobatea as tea import palette fn main() { - mut petal_model := PetalModel.new(palette.theme_bg_color) + mut petal_model := PetalModel.new(palette.theme_bg_color, palette.fg_color(palette.theme_bg_color)) mut app := tea.new_program(mut petal_model) petal_model.app_send = app.send app.run() or { panic('something went wrong! ${err}') } diff --git a/petal.v b/petal.v index 8f8e92db..17936e08 100644 --- a/petal.v +++ b/petal.v @@ -8,6 +8,7 @@ const dot = '•' struct PetalModel { mut: app_send ?fn (tea.Msg) + theme_fg_color ?tea.Color theme_bg_color ?tea.Color first_frame bool active_screen DebuggableModel // all screens are debuggable to help with live, well... debugging @@ -17,9 +18,10 @@ mut: last_resize_height int } -fn PetalModel.new(theme_bg_color ?tea.Color) PetalModel { +fn PetalModel.new(theme_bg_color ?tea.Color, theme_fg_color ?tea.Color) PetalModel { return PetalModel{ theme_bg_color: theme_bg_color + theme_fg_color: theme_fg_color first_frame: true active_screen: SplashScreenModel.new() } @@ -91,6 +93,10 @@ fn (mut m PetalModel) update(msg tea.Msg) (tea.Model, ?tea.Cmd) { fn (mut m PetalModel) view(mut ctx tea.Context) { if m.first_frame { + if fg_color := m.theme_fg_color { + ctx.set_default_fg_color(fg_color) + } + if bg_color := m.theme_bg_color { ctx.set_default_bg_color(bg_color) }