Skip to content

Commit 9c20bea

Browse files
Tools: Remove the unnecessary 'FF' from generated colors (#9594)
* Tools: Remove the unecessary 'FF' from generated colors When colors in most UI tools are 100% alpha the extra 'FF' is auto cut off for readability. e.g. #000000FF becomes #000000. Since introducing the new color pickers this has been the case on the UI side. However the code generator for gradients was inconsistent and would still add in the unnecessary 'FF'. This fixes that and makes generated gradient code tidier. * Fix correct tests * Fix * [autofix.ci] apply automated fixes * Fix * Fix * Fix --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 09dae48 commit 9c20bea

File tree

7 files changed

+36
-17
lines changed

7 files changed

+36
-17
lines changed

api/node/__test__/js_value_conversion.spec.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ test("get/set brush properties", (t) => {
300300

301301
const black = instance!.getProperty("black");
302302

303-
t.is((black as private_api.SlintBrush).toString(), "#000000ff");
303+
t.is((black as private_api.SlintBrush).toString(), "#000000");
304304

305305
if (t.true(black instanceof private_api.SlintBrush)) {
306306
const blackSlintRgbaColor = (black as private_api.SlintBrush).color;

api/node/rust/types/brush.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,18 @@ impl SlintRgbaColor {
159159
/// Returns the color as string in hex representation e.g. `#000000` for black.
160160
#[napi]
161161
pub fn to_string(&self) -> String {
162-
format!("#{:02x}{:02x}{:02x}{:02x}", self.red(), self.green(), self.blue(), self.alpha())
162+
let alpha = self.alpha() as u8;
163+
if alpha == 255 {
164+
format!("#{:02x}{:02x}{:02x}", self.red() as u8, self.green() as u8, self.blue() as u8)
165+
} else {
166+
format!(
167+
"#{:02x}{:02x}{:02x}{:02x}",
168+
self.red() as u8,
169+
self.green() as u8,
170+
self.blue() as u8,
171+
alpha
172+
)
173+
}
163174
}
164175
}
165176

internal/interpreter/json.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ pub fn value_to_json(value: &Value) -> Result<serde_json::Value, String> {
212212
let g = color.green();
213213
let b = color.blue();
214214

215-
format!("#{r:02x}{g:02x}{b:02x}{a:02x}")
215+
if a == 255 {
216+
format!("#{r:02x}{g:02x}{b:02x}")
217+
} else {
218+
format!("#{r:02x}{g:02x}{b:02x}{a:02x}")
219+
}
216220
}
217221

218222
fn gradient_to_string_helper<'a>(
@@ -492,7 +496,7 @@ fn test_to_json() {
492496
0xff, 0x0a, 0xb0, 0xcd,
493497
))))
494498
.unwrap();
495-
assert_eq!(v, "\"#0ab0cdff\"".to_string());
499+
assert_eq!(v, "\"#0ab0cd\"".to_string());
496500

497501
let v = value_to_json_string(&Value::Brush(Brush::LinearGradient(
498502
i_slint_core::graphics::LinearGradientBrush::new(
@@ -515,7 +519,7 @@ fn test_to_json() {
515519
),
516520
)))
517521
.unwrap();
518-
assert_eq!(&v, "\"@linear-gradient(42deg, #ff0000ff 0%, #00ff00ff 50%, #0000ffff 100%)\"");
522+
assert_eq!(&v, "\"@linear-gradient(42deg, #ff0000 0%, #00ff00 50%, #0000ff 100%)\"");
519523

520524
let v = value_to_json_string(&Value::Brush(Brush::RadialGradient(
521525
i_slint_core::graphics::RadialGradientBrush::new_circle(
@@ -537,5 +541,5 @@ fn test_to_json() {
537541
),
538542
)))
539543
.unwrap();
540-
assert_eq!(&v, "\"@radial-gradient(circle, #ff0000ff 0%, #00ff00ff 50%, #0000ffff 100%)\"");
544+
assert_eq!(&v, "\"@radial-gradient(circle, #ff0000 0%, #00ff00 50%, #0000ff 100%)\"");
541545
}

tests/cases/types/brush.slint

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ assert_eq!(t.get_color_brush().color(), green);
6060

6161
```js
6262
var t = new slint.Test({});
63-
assert.equal(t.color_brush.toString(), "#0000ffff");
63+
assert.equal(t.color_brush.toString(), "#0000ff");
6464
assert(t.test);
6565

66-
let green = "#00ff00ff";
66+
let green = "#00ff00";
6767
t.color_brush = "#00ff00"; // css-color-parser2 can't parse #rrggbbaa yet
6868
assert.equal(t.color_brush.toString(), green);
6969

tests/cases/types/color.slint

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,23 @@ assert(t.b1.toString() == t.b4.toString());
180180
assert(t.b1.toString() == t.b5.toString());
181181
assert(t.b1.toString() != t.r5.toString());
182182

183-
let red = "#ff0000ff";
184-
let blue = "#0000ffff";
185-
let g = "#999999ff";
183+
let red = "#ff0000";
184+
let blue = "#0000ff";
185+
let g = "#999999";
186186
assert.equal(t.r1.toString(), red);
187187
assert.equal(t.b1.toString(), blue);
188188
assert.equal(t.g1.toString(), g);
189-
assert.equal(t.y1.toString(), "#ffff00ff");
189+
assert.equal(t.y1.toString(), "#ffff00");
190190

191191
t.g1 = "blue";
192192
assert.equal(t.g1.toString(), t.b1.toString());
193193
t.g1 = "#f00";
194194
assert.equal(t.g1.toString(), t.r1.toString());
195195

196196
assert.equal(t.c1.toString(), "#ff335588");
197-
assert.equal(t.c2.toString(), "#64172aff");
197+
assert.equal(t.c2.toString(), "#64172a");
198198
assert.equal(t.c3.toString(), "#637f28ce");
199-
assert.equal(t.i1.toString(), "#00ff00ff");
199+
assert.equal(t.i1.toString(), "#00ff00");
200200

201201
assert(t.test_rgb);
202202
assert(t.test);

tools/lsp/preview/ui.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,8 +1793,8 @@ export component Tester {{
17931793
..Default::default()
17941794
},
17951795
super::PropertyValue {
1796-
display_string: "#aabbccff".into(),
1797-
code: "\"#aabbccff\"".into(),
1796+
display_string: "#aabbcc".into(),
1797+
code: "\"#aabbcc\"".into(),
17981798
kind: super::PropertyValueKind::Color,
17991799
value_brush: slint::Brush::SolidColor(slint::Color::from_argb_u8(
18001800
0xff, 0xaa, 0xbb, 0xcc,

tools/lsp/preview/ui/brushes.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ pub fn color_to_string(color: slint::Color) -> slint::SharedString {
5353
let g = color.green();
5454
let b = color.blue();
5555

56-
slint::format!("#{r:02x}{g:02x}{b:02x}{a:02x}")
56+
if a == 255 {
57+
slint::format!("#{r:02x}{g:02x}{b:02x}")
58+
} else {
59+
slint::format!("#{r:02x}{g:02x}{b:02x}{a:02x}")
60+
}
5761
}
5862

5963
fn color_to_short_string(color: slint::Color) -> String {

0 commit comments

Comments
 (0)