From 197001a42a88a68454dacf4a1248c064e693d602 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:31:17 +0100 Subject: [PATCH] refactor(csslsrs): Clean up some colors related code (#3) * refactor(csslsrs): Clean up some colors related code * chore: lockfile * fix: use resolver 2 * fix: use get --------- Co-authored-by: Goulven CLEC'H --- Cargo.lock | 121 +++++--------- Cargo.toml | 1 + crates/csslsrs/Cargo.toml | 3 +- crates/csslsrs/src/features/color_parser.rs | 165 +------------------- crates/csslsrs/src/features/colors.rs | 28 +--- 5 files changed, 55 insertions(+), 263 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8be6942..b6feb34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -257,7 +257,7 @@ checksum = "cf95d9c7e6aba67f8fc07761091e93254677f4db9e27197adecebc7039a58722" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -324,18 +324,18 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.21" +version = "4.5.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +checksum = "69371e34337c4c984bbe322360c2547210bf632eb2814bbe78a6e87a2935bd2b" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" -version = "4.5.21" +version = "4.5.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +checksum = "6e24c1b4099818523236a8ca881d2b45db98dadfb4625cf6608c12069fcbbde1" dependencies = [ "anstyle", "clap_lex", @@ -482,8 +482,7 @@ dependencies = [ "console_error_panic_hook", "lsp-types", "palette", - "phf", - "rustc-hash 2.0.0", + "rustc-hash 2.1.0", "serde", "serde-wasm-bindgen", "serde_json", @@ -591,9 +590,9 @@ checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" [[package]] name = "indexmap" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", "hashbrown 0.15.2", @@ -628,10 +627,11 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" dependencies = [ + "once_cell", "wasm-bindgen", ] @@ -763,7 +763,7 @@ dependencies = [ "indexmap", "json-strip-comments", "once_cell", - "rustc-hash 2.0.0", + "rustc-hash 2.1.0", "serde", "serde_json", "simdutf8", @@ -774,8 +774,7 @@ dependencies = [ [[package]] name = "palette" version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cbf71184cc5ecc2e4e1baccdb21026c20e5fc3dcf63028a086131b3ab00b6e6" +source = "git+https://github.com/Ogeon/palette/?rev=234309cdd2f96ac04f034f963c018b98065dcfd1#234309cdd2f96ac04f034f963c018b98065dcfd1" dependencies = [ "approx", "fast-srgb8", @@ -786,13 +785,12 @@ dependencies = [ [[package]] name = "palette_derive" version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5030daf005bface118c096f510ffb781fc28f9ab6a32ab224d8631be6851d30" +source = "git+https://github.com/Ogeon/palette/?rev=234309cdd2f96ac04f034f963c018b98065dcfd1#234309cdd2f96ac04f034f963c018b98065dcfd1" dependencies = [ "by_address", "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -814,31 +812,7 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" dependencies = [ - "phf_macros", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" -dependencies = [ - "phf_shared", - "rand", -] - -[[package]] -name = "phf_macros" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" -dependencies = [ - "phf_generator", "phf_shared", - "proc-macro2", - "quote", - "syn 2.0.89", ] [[package]] @@ -925,21 +899,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" - [[package]] name = "rayon" version = "1.10.0" @@ -1012,9 +971,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustc-hash" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" [[package]] name = "ryu" @@ -1052,7 +1011,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -1089,7 +1048,7 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -1100,7 +1059,7 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -1124,7 +1083,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -1168,9 +1127,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.89" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -1203,7 +1162,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -1235,7 +1194,7 @@ checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", ] [[package]] @@ -1289,9 +1248,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" dependencies = [ "cfg-if", "once_cell", @@ -1300,24 +1259,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1325,28 +1284,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.90", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" [[package]] name = "web-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index c66a4d7..d767148 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,3 @@ [workspace] members = ["crates/*"] +resolver = "2" diff --git a/crates/csslsrs/Cargo.toml b/crates/csslsrs/Cargo.toml index 22da1c2..39d5501 100644 --- a/crates/csslsrs/Cargo.toml +++ b/crates/csslsrs/Cargo.toml @@ -20,8 +20,7 @@ wasm-bindgen = { version = "0.2", optional = true } biome_css_syntax = "0.5.7" biome_rowan = "0.5.7" rustc-hash = "2.0.0" -palette = "0.7.6" -phf = "0.11.2" +palette = { git = "https://github.com/Ogeon/palette/", rev = "234309cdd2f96ac04f034f963c018b98065dcfd1", version = "0.7.6" } [dev-dependencies] criterion = { package = "codspeed-criterion-compat", version = "*" } diff --git a/crates/csslsrs/src/features/color_parser.rs b/crates/csslsrs/src/features/color_parser.rs index 9eaac0f..2bd850d 100644 --- a/crates/csslsrs/src/features/color_parser.rs +++ b/crates/csslsrs/src/features/color_parser.rs @@ -7,159 +7,6 @@ use std::str::FromStr; /// The alpha value for opaque colors pub const OPAQUE: f32 = 1.0; -// `palette`'s named colors map isn't public, so we need to define it ourselves -// See: https://github.com/Ogeon/palette/issues/421 -pub static NAMED_COLORS: phf::Map<&'static str, Srgb> = phf::phf_map! { - "aliceblue" => Srgb::new(240, 248, 255), - "antiquewhite" => Srgb::new(250, 235, 215), - "aqua" => Srgb::new(0, 255, 255), - "aquamarine" => Srgb::new(127, 255, 212), - "azure" => Srgb::new(240, 255, 255), - "beige" => Srgb::new(245, 245, 220), - "bisque" => Srgb::new(255, 228, 196), - "black" => Srgb::new(0, 0, 0), - "blanchedalmond" => Srgb::new(255, 235, 205), - "blue" => Srgb::new(0, 0, 255), - "blueviolet" => Srgb::new(138, 43, 226), - "brown" => Srgb::new(165, 42, 42), - "burlywood" => Srgb::new(222, 184, 135), - "cadetblue" => Srgb::new(95, 158, 160), - "chartreuse" => Srgb::new(127, 255, 0), - "chocolate" => Srgb::new(210, 105, 30), - "coral" => Srgb::new(255, 127, 80), - "cornflowerblue" => Srgb::new(100, 149, 237), - "cornsilk" => Srgb::new(255, 248, 220), - "crimson" => Srgb::new(220, 20, 60), - "cyan" => Srgb::new(0, 255, 255), - "darkblue" => Srgb::new(0, 0, 139), - "darkcyan" => Srgb::new(0, 139, 139), - "darkgoldenrod" => Srgb::new(184, 134, 11), - "darkgray" => Srgb::new(169, 169, 169), - "darkgreen" => Srgb::new(0, 100, 0), - "darkgrey" => Srgb::new(169, 169, 169), - "darkkhaki" => Srgb::new(189, 183, 107), - "darkmagenta" => Srgb::new(139, 0, 139), - "darkolivegreen" => Srgb::new(85, 107, 47), - "darkorange" => Srgb::new(255, 140, 0), - "darkorchid" => Srgb::new(153, 50, 204), - "darkred" => Srgb::new(139, 0, 0), - "darksalmon" => Srgb::new(233, 150, 122), - "darkseagreen" => Srgb::new(143, 188, 143), - "darkslateblue" => Srgb::new(72, 61, 139), - "darkslategray" => Srgb::new(47, 79, 79), - "darkslategrey" => Srgb::new(47, 79, 79), - "darkturquoise" => Srgb::new(0, 206, 209), - "darkviolet" => Srgb::new(148, 0, 211), - "deeppink" => Srgb::new(255, 20, 147), - "deepskyblue" => Srgb::new(0, 191, 255), - "dimgray" => Srgb::new(105, 105, 105), - "dimgrey" => Srgb::new(105, 105, 105), - "dodgerblue" => Srgb::new(30, 144, 255), - "firebrick" => Srgb::new(178, 34, 34), - "floralwhite" => Srgb::new(255, 250, 240), - "forestgreen" => Srgb::new(34, 139, 34), - "fuchsia" => Srgb::new(255, 0, 255), - "gainsboro" => Srgb::new(220, 220, 220), - "ghostwhite" => Srgb::new(248, 248, 255), - "gold" => Srgb::new(255, 215, 0), - "goldenrod" => Srgb::new(218, 165, 32), - "gray" => Srgb::new(128, 128, 128), - "green" => Srgb::new(0, 128, 0), - "greenyellow" => Srgb::new(173, 255, 47), - "grey" => Srgb::new(128, 128, 128), - "honeydew" => Srgb::new(240, 255, 240), - "hotpink" => Srgb::new(255, 105, 180), - "indianred" => Srgb::new(205, 92, 92), - "indigo" => Srgb::new(75, 0, 130), - "ivory" => Srgb::new(255, 255, 240), - "khaki" => Srgb::new(240, 230, 140), - "lavender" => Srgb::new(230, 230, 250), - "lavenderblush" => Srgb::new(255, 240, 245), - "lawngreen" => Srgb::new(124, 252, 0), - "lemonchiffon" => Srgb::new(255, 250, 205), - "lightblue" => Srgb::new(173, 216, 230), - "lightcoral" => Srgb::new(240, 128, 128), - "lightcyan" => Srgb::new(224, 255, 255), - "lightgoldenrodyellow" => Srgb::new(250, 250, 210), - "lightgray" => Srgb::new(211, 211, 211), - "lightgreen" => Srgb::new(144, 238, 144), - "lightgrey" => Srgb::new(211, 211, 211), - "lightpink" => Srgb::new(255, 182, 193), - "lightsalmon" => Srgb::new(255, 160, 122), - "lightseagreen" => Srgb::new(32, 178, 170), - "lightskyblue" => Srgb::new(135, 206, 250), - "lightslategray" => Srgb::new(119, 136, 153), - "lightslategrey" => Srgb::new(119, 136, 153), - "lightsteelblue" => Srgb::new(176, 196, 222), - "lightyellow" => Srgb::new(255, 255, 224), - "lime" => Srgb::new(0, 255, 0), - "limegreen" => Srgb::new(50, 205, 50), - "linen" => Srgb::new(250, 240, 230), - "magenta" => Srgb::new(255, 0, 255), - "maroon" => Srgb::new(128, 0, 0), - "mediumaquamarine" => Srgb::new(102, 205, 170), - "mediumblue" => Srgb::new(0, 0, 205), - "mediumorchid" => Srgb::new(186, 85, 211), - "mediumpurple" => Srgb::new(147, 112, 219), - "mediumseagreen" => Srgb::new(60, 179, 113), - "mediumslateblue" => Srgb::new(123, 104, 238), - "mediumspringgreen" => Srgb::new(0, 250, 154), - "mediumturquoise" => Srgb::new(72, 209, 204), - "mediumvioletred" => Srgb::new(199, 21, 133), - "midnightblue" => Srgb::new(25, 25, 112), - "mintcream" => Srgb::new(245, 255, 250), - "mistyrose" => Srgb::new(255, 228, 225), - "moccasin" => Srgb::new(255, 228, 181), - "navajowhite" => Srgb::new(255, 222, 173), - "navy" => Srgb::new(0, 0, 128), - "oldlace" => Srgb::new(253, 245, 230), - "olive" => Srgb::new(128, 128, 0), - "olivedrab" => Srgb::new(107, 142, 35), - "orange" => Srgb::new(255, 165, 0), - "orangered" => Srgb::new(255, 69, 0), - "orchid" => Srgb::new(218, 112, 214), - "palegoldenrod" => Srgb::new(238, 232, 170), - "palegreen" => Srgb::new(152, 251, 152), - "paleturquoise" => Srgb::new(175, 238, 238), - "palevioletred" => Srgb::new(219, 112, 147), - "papayawhip" => Srgb::new(255, 239, 213), - "peachpuff" => Srgb::new(255, 218, 185), - "peru" => Srgb::new(205, 133, 63), - "pink" => Srgb::new(255, 192, 203), - "plum" => Srgb::new(221, 160, 221), - "powderblue" => Srgb::new(176, 224, 230), - "purple" => Srgb::new(128, 0, 128), - "rebeccapurple" => Srgb::new(102, 51, 153), - "red" => Srgb::new(255, 0, 0), - "rosybrown" => Srgb::new(188, 143, 143), - "royalblue" => Srgb::new(65, 105, 225), - "saddlebrown" => Srgb::new(139, 69, 19), - "salmon" => Srgb::new(250, 128, 114), - "sandybrown" => Srgb::new(244, 164, 96), - "seagreen" => Srgb::new(46, 139, 87), - "seashell" => Srgb::new(255, 245, 238), - "sienna" => Srgb::new(160, 82, 45), - "silver" => Srgb::new(192, 192, 192), - "skyblue" => Srgb::new(135, 206, 235), - "slateblue" => Srgb::new(106, 90, 205), - "slategray" => Srgb::new(112, 128, 144), - "slategrey" => Srgb::new(112, 128, 144), - "snow" => Srgb::new(255, 250, 250), - "springgreen" => Srgb::new(0, 255, 127), - "steelblue" => Srgb::new(70, 130, 180), - "tan" => Srgb::new(210, 180, 140), - "teal" => Srgb::new(0, 128, 128), - "thistle" => Srgb::new(216, 191, 216), - "tomato" => Srgb::new(255, 99, 71), - "turquoise" => Srgb::new(64, 224, 208), - "violet" => Srgb::new(238, 130, 238), - "wheat" => Srgb::new(245, 222, 179), - "white" => Srgb::new(255, 255, 255), - "whitesmoke" => Srgb::new(245, 245, 245), - "yellow" => Srgb::new(255, 255, 0), - "yellowgreen" => Srgb::new(154, 205, 50), -}; - /// A trait that allows converting between CSS colors and LSP colors and serializing the color back to CSS pub trait CSSColor { fn to_lsp_color(&self) -> lsp_types::Color; @@ -212,7 +59,7 @@ impl CSSColor for Srgba { impl CSSColor for Hsla { fn to_lsp_color(&self) -> lsp_types::Color { - let color: Srgba = self.into_format().into_color(); + let color: Srgba = (*self).into_color(); color.to_lsp_color() } @@ -274,8 +121,7 @@ impl CSSColor for Hwba { impl CSSColor for Laba { fn to_lsp_color(&self) -> lsp_types::Color { - let laba_color = Laba::new(self.l, self.a, self.b, self.alpha); - let color: Srgba = laba_color.into_color(); + let color: Srgba = (*self).into_color(); color.to_lsp_color() } @@ -301,8 +147,7 @@ impl CSSColor for Laba { impl CSSColor for Lcha { fn to_lsp_color(&self) -> lsp_types::Color { - let lcha_color = Lcha::new(self.l, self.chroma, self.hue, self.alpha); - let color: Srgba = lcha_color.into_color(); + let color: Srgba = (*self).into_color(); color.to_lsp_color() } @@ -344,11 +189,11 @@ pub fn parse_css_color(text: &str) -> Option> { if let Some(text) = text.strip_prefix('#') { match text.len() { 3 | 6 => { - let color: Srgb = Srgb::from_str(text).unwrap().into(); + let color: Srgb = Srgb::from_str(text).unwrap(); return Some(Box::new(Srgba::from(color))); } 4 | 8 => { - let color: Srgba = Srgba::from_str(text).unwrap().into_format(); + let color: Srgba = Srgba::from_str(text).unwrap(); return Some(Box::new(color)); } _ => return None, diff --git a/crates/csslsrs/src/features/colors.rs b/crates/csslsrs/src/features/colors.rs index 9ce3fdd..4f026b2 100644 --- a/crates/csslsrs/src/features/colors.rs +++ b/crates/csslsrs/src/features/colors.rs @@ -2,7 +2,7 @@ use biome_css_parser::CssParse; use biome_css_syntax::{CssLanguage, CssSyntaxKind}; use biome_rowan::{AstNode, SyntaxNode}; use lsp_types::{ColorInformation, ColorPresentation, Range, TextDocumentItem, TextEdit}; -use palette::{Hsla, Hwba, Laba, Lcha, Srgba, WithAlpha}; +use palette::{named, Hsla, Hwba, Laba, Lcha, Srgba, WithAlpha}; use crate::{ converters::{line_index::LineIndex, to_proto::range, PositionEncoding}, @@ -10,7 +10,7 @@ use crate::{ service::LanguageService, }; -use super::color_parser::{parse_css_color, CSSColor, NAMED_COLORS}; +use super::color_parser::{parse_css_color, CSSColor}; fn extract_colors_information( node: &SyntaxNode, @@ -31,16 +31,7 @@ fn extract_colors_information( if let Some(function_name) = child.first_child().map(|n| n.text().to_string()) { if matches!( function_name.as_str(), - "rgb" - | "rgba" - | "hsl" - | "hsla" - | "hwb" - | "hwba" - | "lab" - | "lch" - | "hsv" - | "hsva" + "rgb" | "rgba" | "hsl" | "hsla" | "hwb" | "hwba" | "lab" | "lch" ) { if let Some(color) = parse_css_color(&node.text().to_string()) { colors.push(ColorInformation { @@ -53,10 +44,8 @@ fn extract_colors_information( } // Any CSS identifier, such as a property name or basic value (ex: `color: red;` contains two identifiers) CssSyntaxKind::CSS_IDENTIFIER => { - if let Some(color) = NAMED_COLORS - .get(&node.text().to_string()) - .map(|color| Srgba::new(color.red, color.green, color.blue, 255).into_format()) - { + if let Some(color) = named::from_str(&node.text().to_string().to_lowercase()) { + let color: Srgba = color.into_format().into(); colors.push(ColorInformation { color: color.to_lsp_color(), range: range(line_index, node.text_range(), encoding).unwrap(), @@ -101,10 +90,9 @@ fn compute_color_presentations(color: ColorInformation, range: Range) -> Vec = vec![]; // Check for a named color with the same values - let named_color = NAMED_COLORS - .values() - .position(|&color| color == rgb_color.into_format::().without_alpha()); - let named_color_string = named_color.map(|i| NAMED_COLORS.keys().nth(i).unwrap()); + let named_color = named::colors() + .position(|color| color == rgb_color.into_format::().without_alpha()); + let named_color_string = named_color.map(|i| named::names().nth(i).unwrap()); if let Some(named_color_string) = named_color_string { color_texts.push(named_color_string.to_string());