-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2055 from matze/perceptual-gradient
Compute gradients in Oklab color space
- Loading branch information
Showing
17 changed files
with
705 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
too-many-arguments-threshold = 20 | ||
enum-variant-name-threshold = 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "gradient" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
iced = { path = "../.." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use iced::gradient; | ||
use iced::widget::{column, container, horizontal_space, row, slider, text}; | ||
use iced::{ | ||
Alignment, Background, Color, Element, Length, Radians, Sandbox, Settings, | ||
}; | ||
|
||
pub fn main() -> iced::Result { | ||
Gradient::run(Settings::default()) | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
struct Gradient { | ||
start: Color, | ||
end: Color, | ||
angle: Radians, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
enum Message { | ||
StartChanged(Color), | ||
EndChanged(Color), | ||
AngleChanged(Radians), | ||
} | ||
|
||
impl Sandbox for Gradient { | ||
type Message = Message; | ||
|
||
fn new() -> Self { | ||
Self { | ||
start: Color::WHITE, | ||
end: Color::new(0.0, 0.0, 1.0, 1.0), | ||
angle: Radians(0.0), | ||
} | ||
} | ||
|
||
fn title(&self) -> String { | ||
String::from("Gradient") | ||
} | ||
|
||
fn update(&mut self, message: Message) { | ||
match message { | ||
Message::StartChanged(color) => self.start = color, | ||
Message::EndChanged(color) => self.end = color, | ||
Message::AngleChanged(angle) => self.angle = angle, | ||
} | ||
} | ||
|
||
fn view(&self) -> Element<Message> { | ||
let Self { start, end, angle } = *self; | ||
|
||
let gradient_box = container(horizontal_space(Length::Fill)) | ||
.width(Length::Fill) | ||
.height(Length::Fill) | ||
.style(move |_: &_| { | ||
let gradient = gradient::Linear::new(angle) | ||
.add_stop(0.0, start) | ||
.add_stop(1.0, end) | ||
.into(); | ||
|
||
container::Appearance { | ||
background: Some(Background::Gradient(gradient)), | ||
..Default::default() | ||
} | ||
}); | ||
|
||
let angle_picker = row![ | ||
text("Angle").width(64), | ||
slider(Radians::RANGE, self.angle, Message::AngleChanged) | ||
.step(0.01) | ||
] | ||
.spacing(8) | ||
.padding(8) | ||
.align_items(Alignment::Center); | ||
|
||
column![ | ||
color_picker("Start", self.start).map(Message::StartChanged), | ||
color_picker("End", self.end).map(Message::EndChanged), | ||
angle_picker, | ||
gradient_box | ||
] | ||
.into() | ||
} | ||
} | ||
|
||
fn color_picker(label: &str, color: Color) -> Element<'_, Color> { | ||
row![ | ||
text(label).width(64), | ||
slider(0.0..=1.0, color.r, move |r| { Color { r, ..color } }) | ||
.step(0.01), | ||
slider(0.0..=1.0, color.g, move |g| { Color { g, ..color } }) | ||
.step(0.01), | ||
slider(0.0..=1.0, color.b, move |b| { Color { b, ..color } }) | ||
.step(0.01), | ||
] | ||
.spacing(8) | ||
.padding(8) | ||
.align_items(Alignment::Center) | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn interpolate_color(from_: vec4<f32>, to_: vec4<f32>, factor: f32) -> vec4<f32> { | ||
return mix(from_, to_, factor); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const to_lms = mat3x4<f32>( | ||
vec4<f32>(0.4121656120, 0.2118591070, 0.0883097947, 0.0), | ||
vec4<f32>(0.5362752080, 0.6807189584, 0.2818474174, 0.0), | ||
vec4<f32>(0.0514575653, 0.1074065790, 0.6302613616, 0.0), | ||
); | ||
|
||
const to_rgb = mat3x4<f32>( | ||
vec4<f32>( 4.0767245293, -3.3072168827, 0.2307590544, 0.0), | ||
vec4<f32>(-1.2681437731, 2.6093323231, -0.3411344290, 0.0), | ||
vec4<f32>(-0.0041119885, -0.7034763098, 1.7068625689, 0.0), | ||
); | ||
|
||
fn interpolate_color(from_: vec4<f32>, to_: vec4<f32>, factor: f32) -> vec4<f32> { | ||
// To Oklab | ||
let lms_a = pow(from_ * to_lms, vec3<f32>(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0)); | ||
let lms_b = pow(to_ * to_lms, vec3<f32>(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0)); | ||
let mixed = mix(lms_a, lms_b, factor); | ||
|
||
// Back to linear RGB | ||
var color = to_rgb * (mixed * mixed * mixed); | ||
|
||
// Alpha interpolation | ||
color.a = mix(from_.a, to_.a, factor); | ||
|
||
return color; | ||
} |
Oops, something went wrong.