neovim-gtk/src/color.rs

98 lines
2.6 KiB
Rust
Raw Normal View History

2017-09-05 14:03:20 +00:00
use std;
2017-08-23 09:45:56 +00:00
use gdk;
2017-08-31 15:37:55 +00:00
use ui_model::Cell;
2017-08-23 09:45:56 +00:00
2017-09-05 14:03:20 +00:00
#[derive(Clone, PartialEq, Debug)]
2017-08-23 09:45:56 +00:00
pub struct Color(pub f64, pub f64, pub f64);
pub const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0);
pub const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0);
pub const COLOR_RED: Color = Color(1.0, 0.0, 0.0);
2017-09-05 14:03:20 +00:00
impl<'a> Into<gdk::RGBA> for &'a Color {
2017-08-23 09:45:56 +00:00
fn into(self) -> gdk::RGBA {
gdk::RGBA {
red: self.0,
green: self.1,
blue: self.2,
alpha: 1.0,
}
}
}
2017-08-31 15:37:55 +00:00
2017-09-05 14:03:20 +00:00
impl Color {
pub fn from_indexed_color(indexed_color: u64) -> Color {
let r = ((indexed_color >> 16) & 0xff) as f64;
let g = ((indexed_color >> 8) & 0xff) as f64;
let b = (indexed_color & 0xff) as f64;
Color(r / 255.0, g / 255.0, b / 255.0)
}
pub fn to_u16(&self) -> (u16, u16, u16) {
(
(std::u16::MAX as f64 * self.0) as u16,
(std::u16::MAX as f64 * self.1) as u16,
(std::u16::MAX as f64 * self.2) as u16,
)
}
}
2017-08-31 15:37:55 +00:00
pub struct ColorModel {
pub bg_color: Color,
pub fg_color: Color,
pub sp_color: Color,
}
impl ColorModel {
pub fn new() -> Self {
2017-09-05 14:03:20 +00:00
ColorModel {
2017-08-31 15:37:55 +00:00
bg_color: COLOR_BLACK,
fg_color: COLOR_WHITE,
sp_color: COLOR_RED,
}
}
2017-09-05 15:23:46 +00:00
pub fn cell_colors<'a>(&'a self, cell: &'a Cell) -> (Option<&'a Color>, &'a Color) {
2017-09-05 14:03:20 +00:00
if !cell.attrs.reverse {
(
2017-09-05 15:23:46 +00:00
cell.attrs.background.as_ref(),
2017-09-05 14:03:20 +00:00
cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color),
)
2017-08-31 15:37:55 +00:00
} else {
2017-09-05 14:03:20 +00:00
(
2017-09-05 15:23:46 +00:00
cell.attrs.foreground.as_ref().or(Some(&self.fg_color)),
2017-09-05 14:03:20 +00:00
cell.attrs.background.as_ref().unwrap_or(&self.bg_color),
)
}
}
2017-08-31 15:37:55 +00:00
2017-09-05 14:03:20 +00:00
pub fn cell_fg<'a>(&'a self, cell: &'a Cell) -> Option<&'a Color> {
if !cell.attrs.reverse {
cell.attrs.foreground.as_ref()
2017-08-31 15:37:55 +00:00
} else {
2017-09-05 14:03:20 +00:00
cell.attrs.background.as_ref().or(Some(&self.bg_color))
2017-08-31 15:37:55 +00:00
}
}
2017-09-06 14:18:30 +00:00
pub fn cell_bg<'a>(&'a self, cell: &'a Cell) -> Option<&'a Color> {
if !cell.attrs.reverse {
cell.attrs.background.as_ref()
} else {
cell.attrs.foreground.as_ref().or(Some(&self.fg_color))
}
}
2017-09-07 15:51:12 +00:00
pub fn actual_cell_bg<'a>(&'a self, cell: &'a Cell) -> &'a Color {
if !cell.attrs.reverse {
cell.attrs.background.as_ref().unwrap_or(&self.bg_color)
} else {
cell.attrs.foreground.as_ref().unwrap_or(&self.fg_color)
}
}
2017-09-09 20:02:06 +00:00
#[inline]
pub fn actual_cell_sp<'a>(&'a self, cell: &'a Cell) -> &'a Color {
cell.attrs.special.as_ref().unwrap_or(&self.sp_color)
}
2017-08-31 15:37:55 +00:00
}