summaryrefslogtreecommitdiffstats
path: root/vendor/nu-ansi-term/src/gradient.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
commit2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35 (patch)
treed325add32978dbdc1db975a438b3a77d571b1ab8 /vendor/nu-ansi-term/src/gradient.rs
parentReleasing progress-linux version 1.68.2+dfsg1-1~progress7.99u1. (diff)
downloadrustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.tar.xz
rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.zip
Merging upstream version 1.69.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/nu-ansi-term/src/gradient.rs')
-rw-r--r--vendor/nu-ansi-term/src/gradient.rs105
1 files changed, 105 insertions, 0 deletions
diff --git a/vendor/nu-ansi-term/src/gradient.rs b/vendor/nu-ansi-term/src/gradient.rs
new file mode 100644
index 000000000..a0d94c8cd
--- /dev/null
+++ b/vendor/nu-ansi-term/src/gradient.rs
@@ -0,0 +1,105 @@
+use crate::{rgb::Rgb, Color};
+
+/// Linear color gradient between two color stops
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct Gradient {
+ /// Start Color of Gradient
+ pub start: Rgb,
+
+ /// End Color of Gradient
+ pub end: Rgb,
+}
+
+impl Gradient {
+ /// Creates a new [Gradient] with two [Rgb] colors, `start` and `end`
+ #[inline]
+ pub const fn new(start: Rgb, end: Rgb) -> Self {
+ Self { start, end }
+ }
+ pub const fn from_color_rgb(start: Color, end: Color) -> Self {
+ let start_grad = match start {
+ Color::Rgb(r, g, b) => Rgb { r, g, b },
+ _ => Rgb { r: 0, g: 0, b: 0 },
+ };
+ let end_grad = match end {
+ Color::Rgb(r, g, b) => Rgb { r, g, b },
+ _ => Rgb { r: 0, g: 0, b: 0 },
+ };
+
+ Self {
+ start: start_grad,
+ end: end_grad,
+ }
+ }
+
+ /// Computes the [Rgb] color between `start` and `end` for `t`
+ pub fn at(&self, t: f32) -> Rgb {
+ self.start.lerp(self.end, t)
+ }
+
+ /// Returns the reverse of `self`
+ #[inline]
+ pub const fn reverse(&self) -> Self {
+ Self::new(self.end, self.start)
+ }
+
+ #[allow(dead_code)]
+ pub fn build(&self, text: &str, target: TargetGround) -> String {
+ let delta = 1.0 / text.len() as f32;
+ let mut result = text.char_indices().fold(String::new(), |mut acc, (i, c)| {
+ let temp = format!(
+ "\x1B[{}m{}",
+ self.at(i as f32 * delta).ansi_color_code(target),
+ c
+ );
+ acc.push_str(&temp);
+ acc
+ });
+
+ result.push_str("\x1B[0m");
+ result
+ }
+}
+
+#[allow(dead_code)]
+pub fn build_all_gradient_text(text: &str, foreground: Gradient, background: Gradient) -> String {
+ let delta = 1.0 / text.len() as f32;
+ let mut result = text.char_indices().fold(String::new(), |mut acc, (i, c)| {
+ let step = i as f32 * delta;
+ let temp = format!(
+ "\x1B[{};{}m{}",
+ foreground
+ .at(step)
+ .ansi_color_code(TargetGround::Foreground),
+ background
+ .at(step)
+ .ansi_color_code(TargetGround::Background),
+ c
+ );
+ acc.push_str(&temp);
+ acc
+ });
+
+ result.push_str("\x1B[0m");
+ result
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum TargetGround {
+ Foreground,
+ Background,
+}
+
+impl TargetGround {
+ #[inline]
+ pub const fn code(&self) -> u8 {
+ match self {
+ Self::Foreground => 30,
+ Self::Background => 40,
+ }
+ }
+}
+
+pub trait ANSIColorCode {
+ fn ansi_color_code(&self, target: TargetGround) -> String;
+}