ansi_colours-1.2.3/.cargo_vcs_info.json0000644000000001360000000000100135370ustar { "git": { "sha1": "3714236bbcf61f1972b9e9f466c4f64f8cfb36c8" }, "path_in_vcs": "" }ansi_colours-1.2.3/.gitignore000064400000000000000000000000451046102023000143160ustar 00000000000000*.o /target /tools/target Cargo.lock ansi_colours-1.2.3/.rustfmt.toml000064400000000000000000000007311046102023000150070ustar 00000000000000binop_separator = "Back" blank_lines_lower_bound = 0 blank_lines_upper_bound = 3 brace_style = "PreferSameLine" color = "Auto" condense_wildcard_suffixes = true fn_single_line = true format_macro_matchers = true format_strings = true group_imports = "StdExternalCrate" max_width = 80 newline_style = "Unix" normalize_doc_attributes = true overflow_delimited_expr = true reorder_imports = true reorder_modules = true use_field_init_shorthand = true use_try_shorthand = true ansi_colours-1.2.3/.travis.yml000064400000000000000000000002641046102023000144420ustar 00000000000000language: rust rust: - stable matrix: fast_finish: true script: - cargo test --verbose - cargo test --verbose --all-features - cargo test --verbose --no-default-features ansi_colours-1.2.3/Cargo.lock0000644000000330340000000000100115150ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "aho-corasick" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] [[package]] name = "anes" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "ansi_colours" version = "1.2.3" dependencies = [ "ansi_term", "anstyle", "crc64", "criterion", "empfindung", "lab", "rgb", "termcolor", ] [[package]] name = "ansi_term" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ "winapi", ] [[package]] name = "anstyle" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "bytemuck" version = "1.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e" [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "ciborium" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" dependencies = [ "ciborium-io", "ciborium-ll", "serde", ] [[package]] name = "ciborium-io" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" [[package]] name = "ciborium-ll" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" dependencies = [ "ciborium-io", "half", ] [[package]] name = "clap" version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstyle", "clap_lex", ] [[package]] name = "clap_lex" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "crc64" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2707e3afba5e19b75d582d88bc79237418f2a2a2d673d01cf9b03633b46e98f3" [[package]] name = "criterion" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" dependencies = [ "anes", "cast", "ciborium", "clap", "criterion-plot", "is-terminal", "itertools", "num-traits", "once_cell", "oorandom", "regex", "serde", "serde_derive", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", "itertools", ] [[package]] name = "crunchy" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "empfindung" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8af095ec32b4a626b56a2e188515e4e5480d82b02e5d8a830fdb61934226b0df" dependencies = [ "lab", "rgb", ] [[package]] name = "half" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" dependencies = [ "cfg-if", "crunchy", ] [[package]] name = "hermit-abi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "is-terminal" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ "hermit-abi", "libc", "windows-sys", ] [[package]] name = "itertools" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "lab" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf36173d4167ed999940f804952e6b08197cae5ad5d572eb4db150ce8ad5d58f" [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "oorandom" version = "11.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" [[package]] name = "proc-macro2" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "regex" version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "rgb" version = "0.8.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ade4539f42266ded9e755c605bdddf546242b2c961b03b06a7375260788a0523" dependencies = [ "bytemuck", ] [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", "serde", ] [[package]] name = "syn" version = "2.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "termcolor" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" dependencies = [ "winapi-util", ] [[package]] name = "tinytemplate" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ "serde", "serde_json", ] [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ "windows-sys", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" ansi_colours-1.2.3/Cargo.toml0000644000000031430000000000100115360ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "ansi_colours" version = "1.2.3" authors = ["Michał Nazarewicz "] exclude = ["tools/**"] description = "true-colour ↔ ANSI terminal palette converter" documentation = "https://docs.rs/ansi_colours" readme = "README.md" keywords = [ "ansi", "terminal", "color", "rgb", ] categories = ["command-line-interface"] license = "LGPL-3.0-or-later" repository = "https://github.com/mina86/ansi_colours" [package.metadata.docs.rs] all-features = true [profile.release] lto = true panic = "abort" [[bench]] name = "ansi256" harness = false [dependencies.ansi_term] version = "0.12" optional = true [dependencies.anstyle] version = "1" optional = true [dependencies.rgb] version = "0.8" optional = true [dependencies.termcolor] version = ">= 1.0, <= 1.2" optional = true [dev-dependencies.crc64] version = "2" [dev-dependencies.criterion] version = "0.5" default-features = false [dev-dependencies.empfindung] version = "0.2.6" [dev-dependencies.lab] version = ">=0.4, <1.0" [dev-dependencies.rgb] version = "0.8" [features] default = ["rgb"] [badges.maintenance] status = "actively-developed" ansi_colours-1.2.3/Cargo.toml.orig0000644000000021010000000000100124660ustar [package] name = "ansi_colours" description = "true-colour ↔ ANSI terminal palette converter" version = "1.2.3" readme = "README.md" authors = ["Michał Nazarewicz "] keywords = ["ansi", "terminal", "color", "rgb"] categories = ["command-line-interface"] license = "LGPL-3.0-or-later" repository = "https://github.com/mina86/ansi_colours" documentation = "https://docs.rs/ansi_colours" exclude = ["tools/**"] edition = "2018" [package.metadata.docs.rs] all-features = true [badges] maintenance = { status = "actively-developed" } [dependencies] ansi_term = { version = "0.12", optional = true } anstyle = { version = "1", optional = true } rgb = { version = "0.8", optional = true } termcolor = { version = ">= 1.0, <= 1.2", optional = true } [features] default = ["rgb"] [dev-dependencies] crc64 = "2" criterion = { version = "0.5", default-features = false } empfindung = "0.2.6" lab = ">=0.4, <1.0" rgb = "0.8" [profile.release] lto = true panic = 'abort' [[bench]] name = "ansi256" harness = false ansi_colours-1.2.3/Cargo.toml.orig000064400000000000000000000021011046102023000152100ustar 00000000000000[package] name = "ansi_colours" description = "true-colour ↔ ANSI terminal palette converter" version = "1.2.3" readme = "README.md" authors = ["Michał Nazarewicz "] keywords = ["ansi", "terminal", "color", "rgb"] categories = ["command-line-interface"] license = "LGPL-3.0-or-later" repository = "https://github.com/mina86/ansi_colours" documentation = "https://docs.rs/ansi_colours" exclude = ["tools/**"] edition = "2018" [package.metadata.docs.rs] all-features = true [badges] maintenance = { status = "actively-developed" } [dependencies] ansi_term = { version = "0.12", optional = true } anstyle = { version = "1", optional = true } rgb = { version = "0.8", optional = true } termcolor = { version = ">= 1.0, <= 1.2", optional = true } [features] default = ["rgb"] [dev-dependencies] crc64 = "2" criterion = { version = "0.5", default-features = false } empfindung = "0.2.6" lab = ">=0.4, <1.0" rgb = "0.8" [profile.release] lto = true panic = 'abort' [[bench]] name = "ansi256" harness = false ansi_colours-1.2.3/LICENSE000064400000000000000000000167441046102023000133500ustar 00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ansi_colours-1.2.3/README.md000064400000000000000000000054321046102023000136120ustar 00000000000000# True-colour ↔ ANSI terminal palette converter [![crates.io](https://img.shields.io/crates/v/ansi_colours)](https://crates.io/crates/ansi_colours) [![Docs](https://docs.rs/ansi_colours/badge.svg)](https://docs.rs/ansi_colours) [![License](https://img.shields.io/badge/license-LGPL-blue.svg)](https://github.com/mina86/ansi_colours/blob/master/LICENSE) `ansi_colours` converts between 24-bit sRGB colours and 8-bit colour palette used by ANSI terminals such as xterm or rxvt-unicode in 256-colour mode. The most common use case is when using 24-bit colours in a terminal emulator which only support 8-bit colour palette. It allows true-colours to be approximated by values supported by the terminal. When mapping true-colour into available 256-colour palette, it tries to balance accuracy and performance. It doesn’t implement the fastest algorithm nor is it the most accurate, instead it uses a formula which should be fast enough and accurate enough for most use-cases. ## Usage The algorithm has C and Rust implementations and can be easily used from C, C++ or Rust. The two implementations are equivalent and are provided for best performance. Since version 1.0.4 the Rust crate has sped up by 25% when doing True Colour → ANSI index conversion and 75% when doing conversion in the other direction. ### Rust Using this package with Cargo projects is as simple as adding a single dependency: ```toml [dependencies] ansi_colours = "1.2" ``` and then using one of functions that the library provides: ```rust use ansi_colours::*; fn main() { // Colour at given index: println!("{:-3}: {:?}", 50, rgb_from_ansi256(50)); // Approximate true-colour by colour in the palette: let rgb = (100, 200, 150); let index = ansi256_from_rgb(rgb); println!("{:?} ~ {:-3} {:?}", rgb, index, rgb_from_ansi256(index)); } ``` To facilitate better interoperability the crate defines `rgb` (enabled by default), `ansi_term`, `anstyle`, and `termcolor` cargo features which add support for crates with the same name. ### C and C++ The easiest way to use this library in C or C++ is to copy the `ansi_colour.h` and `ansi256.c` files to your project, set up compilation step for the `ansi256.c` file, add the header file to the include path and once all that is done use the two provided functions: ```c #include #include "ansi_colours.h" int main() { // Colour at given index: printf("%-3u #%06x\n", 50, rgb_from_ansi256(50)); // Approximate true-colour by colour in the palette: uint32_t rgb = 0x64C896; uint8_t index = ansi256_from_rgb(rgb); printf("#%06x ~ %-3u %06x\n", rgb, index, rgb_from_ansi256(index)); return 0; } ``` Unfortunately neither C nor C++ ecosystem has a centralised package distribution service so there currently is no more convenient solution. ansi_colours-1.2.3/benches/ansi256.rs000064400000000000000000000012301046102023000154670ustar 00000000000000use criterion::{criterion_group, criterion_main}; fn from_rgb(c: &mut criterion::Criterion) { c.bench_function("convert from True Colour", move |b| { b.iter(|| { for rgb in 0..(1 << 24) { criterion::black_box(ansi_colours::ansi256_from_rgb(rgb)); } }) }); } fn to_rgb(c: &mut criterion::Criterion) { c.bench_function("convert to True Colour", move |b| { b.iter(|| { for idx in 0..256 { criterion::black_box(ansi_colours::rgb_from_ansi256(idx as u8)); } }) }); } criterion_group!(benches, from_rgb, to_rgb); criterion_main!(benches); ansi_colours-1.2.3/examples/convert.rs000064400000000000000000000012611046102023000161730ustar 00000000000000extern crate ansi_colours; use ansi_colours::*; fn main() { let args: Vec = std::env::args().collect(); if args.len() == 2 { let index = args[1].parse::().unwrap(); println!("{:-3}: {:?}", index, rgb_from_ansi256(index)); } else if args.len() == 4 { let rgb = ( args[1].parse::().unwrap(), args[2].parse::().unwrap(), args[3].parse::().unwrap(), ); let index = ansi256_from_rgb(rgb); println!("{:?} ~ {:-3} {:?}", rgb, index, rgb_from_ansi256(index)); } else { eprintln!("usage: convert ( | )"); std::process::exit(1); } } ansi_colours-1.2.3/src/ansi256.rs000064400000000000000000000237561046102023000146700ustar 00000000000000// ansi_colours – true-colour ↔ ANSI terminal palette converter // Copyright 2018 by Michał Nazarewicz // // ansi_colours is free software: you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation; either version 3 of the License, or (at // your option) any later version. // // ansi_colours is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser // General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with ansi_colours. If not, see . /// The ANSI colour palette. #[rustfmt::skip] pub(crate) static ANSI_COLOURS: [u32; 256] = [ // The 16 system colours as used by default by xterm. Taken // from XTerm-col.ad distributed with xterm source code. 0x000000, 0xcd0000, 0x00cd00, 0xcdcd00, 0x0000ee, 0xcd00cd, 0x00cdcd, 0xe5e5e5, 0x7f7f7f, 0xff0000, 0x00ff00, 0xffff00, 0x5c5cff, 0xff00ff, 0x00ffff, 0xffffff, // 6×6×6 cube. One each axis, the six indices map to [0, 95, 135, 175, // 215, 255] RGB component values. 0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f, 0x005f87, 0x005faf, 0x005fd7, 0x005fff, 0x008700, 0x00875f, 0x008787, 0x0087af, 0x0087d7, 0x0087ff, 0x00af00, 0x00af5f, 0x00af87, 0x00afaf, 0x00afd7, 0x00afff, 0x00d700, 0x00d75f, 0x00d787, 0x00d7af, 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f, 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff, 0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af, 0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f, 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff, 0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f, 0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff, 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af, 0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f, 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff, 0x870000, 0x87005f, 0x870087, 0x8700af, 0x8700d7, 0x8700ff, 0x875f00, 0x875f5f, 0x875f87, 0x875faf, 0x875fd7, 0x875fff, 0x878700, 0x87875f, 0x878787, 0x8787af, 0x8787d7, 0x8787ff, 0x87af00, 0x87af5f, 0x87af87, 0x87afaf, 0x87afd7, 0x87afff, 0x87d700, 0x87d75f, 0x87d787, 0x87d7af, 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f, 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff, 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af, 0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f, 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff, 0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f, 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff, 0xafd700, 0xafd75f, 0xafd787, 0xafd7af, 0xafd7d7, 0xafd7ff, 0xafff00, 0xafff5f, 0xafff87, 0xafffaf, 0xafffd7, 0xafffff, 0xd70000, 0xd7005f, 0xd70087, 0xd700af, 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f, 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff, 0xd78700, 0xd7875f, 0xd78787, 0xd787af, 0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f, 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff, 0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f, 0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff, 0xff0000, 0xff005f, 0xff0087, 0xff00af, 0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f, 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff, 0xff8700, 0xff875f, 0xff8787, 0xff87af, 0xff87d7, 0xff87ff, 0xffaf00, 0xffaf5f, 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff, 0xffd700, 0xffd75f, 0xffd787, 0xffd7af, 0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f, 0xffff87, 0xffffaf, 0xffffd7, 0xffffff, // Greyscale ramp. This is calculated as (index - 232) * 10 + 8 // repeated for each RGB component. 0x080808, 0x121212, 0x1c1c1c, 0x262626, 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e, 0x585858, 0x626262, 0x6c6c6c, 0x767676, 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e, 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6, 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee, ]; /// A lookup table for approximations of shades of grey. Values chosen to get /// smallest possible ΔE*₀₀. /// /// Calculating the mapping has several corner cases. The greyscale ramp starts /// at rgb(8, 8, 8) but ends at rgb(238, 238, 238) resulting in asymmetric /// distance to the extreme values. Shades of grey are present in the greyscale /// ramp as well as the 6×6×6 colour cube making it necessary to consider /// multiple cases. And that all on top of ANSI palette using linear indexes in /// gamma encoded colour space. /// /// Not to have to deal with all that, the colours are simply precalculated. /// This way we know we always get the best possible match. This also makes /// conversion for grey colours blazing fast. /// /// There’s a unit test that verifies that those are the best indexes. #[rustfmt::skip] pub(crate) static ANSI256_FROM_GREY: [u8; 256] = [ 16, 16, 16, 16, 16, 232, 232, 232, 232, 232, 232, 232, 232, 232, 233, 233, 233, 233, 233, 233, 233, 233, 233, 233, 234, 234, 234, 234, 234, 234, 234, 234, 234, 234, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 236, 236, 236, 236, 236, 236, 236, 236, 236, 236, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 238, 238, 238, 238, 238, 238, 238, 238, 238, 238, 239, 239, 239, 239, 239, 239, 239, 239, 239, 239, 240, 240, 240, 240, 240, 240, 240, 240, 59, 59, 59, 59, 59, 241, 241, 241, 241, 241, 241, 241, 242, 242, 242, 242, 242, 242, 242, 242, 242, 242, 243, 243, 243, 243, 243, 243, 243, 243, 243, 244, 244, 244, 244, 244, 244, 244, 244, 244, 102, 102, 102, 102, 102, 245, 245, 245, 245, 245, 245, 246, 246, 246, 246, 246, 246, 246, 246, 246, 246, 247, 247, 247, 247, 247, 247, 247, 247, 247, 247, 248, 248, 248, 248, 248, 248, 248, 248, 248, 145, 145, 145, 145, 145, 249, 249, 249, 249, 249, 249, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251, 251, 251, 251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 252, 188, 188, 188, 188, 188, 253, 253, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 231, 231, 231, 231, 231, 231, 231, 231, 231, ]; fn to_triple(rgb: u32) -> (u8, u8, u8) { ((rgb >> 16) as u8, (rgb >> 8) as u8, rgb as u8) } /// Returns index of a colour in 256-colour ANSI palette approximating given /// sRGB colour. #[inline] pub(crate) fn ansi256_from_rgb(rgb: u32) -> u8 { let (r, g, b) = to_triple(rgb); let grey_index = ANSI256_FROM_GREY[luminance(r, g, b) as usize]; let grey_distance = distance((r, g, b), ANSI_COLOURS[grey_index as usize]); let (cube_index, cube_rgb) = cube_index(r, g, b); if distance((r, g, b), cube_rgb) < grey_distance { cube_index } else { grey_index } } fn cube_index(r: u8, g: u8, b: u8) -> (u8, u32) { let r = cube_index_red(r); let g = cube_index_green(g); let b = cube_index_blue(b); (r.0 + g.0 + b.0, r.1 + g.1 + b.1) } #[rustfmt::skip] fn cube_thresholds(v: u8, a: u8, b: u8, c: u8, d: u8, e: u8) -> (u8, u32) { if v < a { (0, 0) } else if v < b { (1, 95) } else if v < c { (2, 135) } else if v < d { (3, 175) } else if v < e { (4, 215) } else { (5, 255) } } // The next three functions approximate a pure colour by an entry in the 6×6×6 // cube. E.g. cube_index_red(r) approximates an rgb(r, 0, 0) colour. This was // motivated by ΔE*₀₀ being most variable in dark colours so I felt it’s more // important to better approximate dark colours than light colours. fn cube_index_red(v: u8) -> (u8, u32) { let (i, v) = cube_thresholds(v, 38, 115, 155, 196, 235); (i * 36 + 16, v << 16) } fn cube_index_green(v: u8) -> (u8, u32) { let (i, v) = cube_thresholds(v, 36, 116, 154, 195, 235); (i * 6, v << 8) } fn cube_index_blue(v: u8) -> (u8, u32) { cube_thresholds(v, 35, 115, 155, 195, 235) } /// Returns luminance of given sRGB colour. The calculation favours speed over /// precision and so doesn’t correctly account for sRGB’s gamma correction. fn luminance(r: u8, g: u8, b: u8) -> u8 { // The following weighted average is as fast as naive arithmetic mean and at // the same time noticeably more precise. The coefficients are the second // row of the RGB->XYZ conversion matrix (i.e. values for calculating Y from // linear RGB) which I’ve calculated so that denominator is 2^24 to simplify // division. let v = 3567664u32 * (r as u32) + 11998547u32 * (g as u32) + 1211005u32 * (b as u32); // Round to nearest rather than truncating when dividing. ((v + (1u32 << 23)) >> 24) as u8 // Approximating sRGB gamma correction with a simple γ=2 improves the // precision considerably but is also five times slower than the above // (and probably slower still on architectures lacking MMS or FPU). // // return sqrtf((float)r * (float)r * 0.2126729f + // (float)g * (float)g * 0.7151521f + // (float)b * (float)b * 0.0721750); // // Doing proper gamma correction results in further improvement but is // also 20 times slower, so we’re opting out from doing that. } /// Calculates distance between two colours. Tries to balance speed and /// perceptual correctness. It’s not a proper metric but two properties this /// function provides are: d(x, x) = 0 and d(x, y) < d(x, z) implies x being /// closer to y than to z. fn distance((xr, xg, xb): (u8, u8, u8), y: u32) -> u32 { let (yr, yg, yb) = to_triple(y); // See though we’re doing a few // things to avoid some of the calculations. We can do that since we only // care about some properties of the metric. let r_sum = (xr as i32) + (yr as i32); let r = (xr as i32) - (yr as i32); let g = (xg as i32) - (yg as i32); let b = (xb as i32) - (yb as i32); let d = (1024 + r_sum) * r * r + 2048 * g * g + (1534 - r_sum) * b * b; d as u32 } ansi_colours-1.2.3/src/impls.rs000064400000000000000000000542601046102023000146170ustar 00000000000000use crate::*; /// Representation of an RGB colour as 24-bit `0xRRGGBB` integer. impl AsRGB for u32 { fn as_u32(&self) -> u32 { *self } } #[inline] fn to_u32(r: u8, g: u8, b: u8) -> u32 { ((r as u32) << 16) | ((g as u32) << 8) | (b as u32) } impl AsRGB for (u8, u8, u8) { #[inline] fn as_u32(&self) -> u32 { to_u32(self.0, self.1, self.2) } } impl AsRGB for [u8; 3] { #[inline] fn as_u32(&self) -> u32 { to_u32(self[0], self[1], self[2]) } } impl<'a, T: AsRGB + ?Sized> AsRGB for &'a T { fn as_u32(&self) -> u32 { (*self).as_u32() } } #[cfg(feature = "rgb")] trait Component: Copy { fn into_u8(self) -> u8; } #[cfg(feature = "rgb")] impl Component for u8 { #[inline(always)] fn into_u8(self) -> u8 { self } } #[cfg(feature = "rgb")] impl Component for u16 { #[inline(always)] fn into_u8(self) -> u8 { (self >> 8) as u8 } } #[cfg(feature = "rgb")] impl AsRGB for rgb::RGB { /// Returns representation of the sRGB colour as a 24-bit `0xRRGGBB` /// integer. /// /// This implementation is present only if `rgb` crate feature is enabled. /// Implementation is provided for `u8` and `u16` colour component types. /// /// # Examples /// /// ``` /// use ansi_colours::{AsRGB, ansi256_from_rgb}; /// /// assert_eq!(0x123456, rgb::RGB8::new(0x12, 0x34, 0x56).as_u32()); /// /// assert_eq!( 16, ansi256_from_rgb(rgb::RGB8::new( 1, 1, 1))); /// assert_eq!( 16, ansi256_from_rgb(rgb::RGB8::new( 0, 1, 2))); /// assert_eq!( 67, ansi256_from_rgb(rgb::RGB8::new( 95, 135, 175))); /// assert_eq!(231, ansi256_from_rgb(rgb::RGB8::new(255, 255, 255))); /// /// assert_eq!(0x123456, rgb::RGB16::new(0x12ab, 0x34cd, 0x56ef).as_u32()); /// /// assert_eq!( 16, ansi256_from_rgb(rgb::RGB16::new( 256, 511, 256))); /// assert_eq!( 16, ansi256_from_rgb(rgb::RGB16::new( 128, 256, 512))); /// assert_eq!( 67, ansi256_from_rgb(rgb::RGB16::new(24500, 34600, 44800))); /// assert_eq!(231, ansi256_from_rgb(rgb::RGB16::new(65535, 65535, 65535))); /// ``` #[inline(always)] fn as_u32(&self) -> u32 { to_u32(self.r.into_u8(), self.g.into_u8(), self.b.into_u8()) } } #[cfg(feature = "rgb")] impl AsRGB for rgb::alt::Gray { #[inline(always)] fn as_u32(&self) -> u32 { self.into_u8() as u32 * 0x010101 } /// Returns index of a colour in 256-colour ANSI palette approximating given /// shade of grey. /// /// This implementation is present only if `rgb` crate feature is enabled. /// Implementation is provided for `u8` and `u16` colour component types. /// /// # Examples /// /// ``` /// use ansi_colours::ansi256_from_rgb; /// /// assert_eq!(244, ansi256_from_rgb(rgb::alt::Gray::(128))); /// assert_eq!(244, ansi256_from_rgb(rgb::alt::Gray::(33023))); /// ``` #[inline(always)] fn to_ansi256(&self) -> u8 { crate::ansi256_from_grey(self.into_u8()) } } #[cfg(feature = "rgb")] impl AsRGB for rgb::alt::BGR { /// Returns representation of the sRGB colour as a 24-bit `0xRRGGBB` /// integer. /// /// This implementation is present only if `rgb` crate feature is enabled. /// Implementation is provided for `u8` and `u16` colour component types. /// /// # Examples /// /// ``` /// use ansi_colours::{AsRGB, ansi256_from_rgb}; /// use rgb::alt::{BGR8, BGR16}; /// /// assert_eq!(0x123456, BGR8 { b: 0x56, g: 0x34, r: 0x12 }.as_u32()); /// /// assert_eq!( 16, ansi256_from_rgb(BGR8 { r: 1, g: 1, b: 1 })); /// assert_eq!( 16, ansi256_from_rgb(BGR8 { r: 0, g: 1, b: 2 })); /// assert_eq!( 67, ansi256_from_rgb(BGR8 { r: 95, g: 135, b: 175 })); /// assert_eq!(231, ansi256_from_rgb(BGR8 { r: 255, g: 255, b: 255 })); /// /// assert_eq!(0x123456, BGR16 { b: 0x56ef, g: 0x34cd, r: 0x12ab }.as_u32()); /// /// assert_eq!( 16, ansi256_from_rgb(BGR16 { r: 256, g: 511, b: 256 })); /// assert_eq!( 16, ansi256_from_rgb(BGR16 { r: 128, g: 256, b: 512 })); /// assert_eq!( 67, ansi256_from_rgb(BGR16 { r: 24500, g: 34600, b: 44800 })); /// assert_eq!(231, ansi256_from_rgb(BGR16 { r: 65535, g: 65535, b: 65535 })); /// ``` #[inline(always)] fn as_u32(&self) -> u32 { to_u32(self.r.into_u8(), self.g.into_u8(), self.b.into_u8()) } } #[cfg(feature = "ansi_term")] impl AsRGB for ansi_term::Colour { /// Returns sRGB colour corresponding to escape code represented by the /// object. /// /// Behaves slightly differently depending on the variant of the enum. /// - For named colour variants (`Black`, `Red` etc. up till `White`), /// returns corresponding system colour with indexes going from 0 to 7. /// - For `Fixed` variant returns colour corresponding to specified index. /// See [`rgb_from_ansi256`](`rgb_from_ansi256`). /// - For `RGB` variant converts it to 24-bit `0xRRGGBB` representation. /// /// This implementation is present only if `ansi_term` crate feature is /// enabled. #[inline] fn as_u32(&self) -> u32 { match *self { Self::Black => ansi256::ANSI_COLOURS[0], Self::Red => ansi256::ANSI_COLOURS[1], Self::Green => ansi256::ANSI_COLOURS[2], Self::Yellow => ansi256::ANSI_COLOURS[3], Self::Blue => ansi256::ANSI_COLOURS[4], Self::Purple => ansi256::ANSI_COLOURS[5], Self::Cyan => ansi256::ANSI_COLOURS[6], Self::White => ansi256::ANSI_COLOURS[7], Self::Fixed(idx) => ansi256::ANSI_COLOURS[usize::from(idx)], Self::RGB(r, g, b) => (r, g, b).as_u32(), } } /// Returns index of a colour in 256-colour ANSI palette approximating given /// sRGB colour. /// /// Behaves slightly differently depending on the variant of the enum. /// - For named colour variants (`Black`, `Red` etc. up till `White`), /// returns index going from 0 to 7. /// - For `Fixed` variant simply returns index encoded in the variant. /// - Lastly, for `RGB` variant, approximates the colour and returns index /// of closest colour in 256-colour palette. /// /// This implementation is present only if `ansi_term` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::AsRGB; /// /// assert_eq!( 0, ansi_term::Colour::Black.to_ansi256()); /// assert_eq!( 7, ansi_term::Colour::White.to_ansi256()); /// assert_eq!( 42, ansi_term::Colour::Fixed(42).to_ansi256()); /// assert_eq!( 16, ansi_term::Colour::RGB( 0, 0, 0).to_ansi256()); /// assert_eq!( 16, ansi_term::Colour::RGB( 1, 1, 1).to_ansi256()); /// assert_eq!( 16, ansi_term::Colour::RGB( 0, 1, 2).to_ansi256()); /// assert_eq!( 67, ansi_term::Colour::RGB( 95, 135, 175).to_ansi256()); /// assert_eq!(231, ansi_term::Colour::RGB(255, 255, 255).to_ansi256()); /// ``` #[inline] fn to_ansi256(&self) -> u8 { match *self { Self::Black => 0, Self::Red => 1, Self::Green => 2, Self::Yellow => 3, Self::Blue => 4, Self::Purple => 5, Self::Cyan => 6, Self::White => 7, Self::Fixed(idx) => idx, Self::RGB(r, g, b) => (r, g, b).to_ansi256(), } } } #[cfg(feature = "ansi_term")] impl super::ColourExt for ansi_term::Colour { /// Constructs a `Fixed` colour which approximates given sRGB colour. /// /// This implementation is present only if `ansi_term` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use ansi_term::Colour; /// /// assert_eq!(Colour::Fixed( 16), Colour::approx_rgb( 0, 0, 0)); /// assert_eq!(Colour::Fixed( 16), Colour::approx_rgb( 0, 1, 2)); /// assert_eq!(Colour::Fixed( 67), Colour::approx_rgb( 95, 135, 175)); /// assert_eq!(Colour::Fixed(231), Colour::approx_rgb(255, 255, 255)); /// ``` #[inline] fn approx_rgb(r: u8, g: u8, b: u8) -> Self { Self::Fixed(ansi256_from_rgb((r, g, b))) } /// Converts the colour into 256-colour-compatible format. /// /// If the colour represents an RGB colour, converts it into a `Fixed` /// variant using [`ansi256_from_rgb`] function. Otherwise, returns the /// colour unchanged. /// /// This implementation is present only if `ansi_term` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use ansi_term::Colour; /// /// assert_eq!(Colour::Red, Colour::Red.to_256()); /// assert_eq!(Colour::Fixed( 11), Colour::Fixed(11).to_256()); /// assert_eq!(Colour::Fixed( 16), Colour::RGB( 0, 0, 0).to_256()); /// assert_eq!(Colour::Fixed( 16), Colour::RGB( 0, 1, 2).to_256()); /// assert_eq!(Colour::Fixed( 67), Colour::RGB( 95, 135, 175).to_256()); /// assert_eq!(Colour::Fixed(231), Colour::RGB(255, 255, 255).to_256()); /// ``` #[inline] fn to_256(&self) -> Self { match *self { Self::RGB(r, g, b) => Self::approx_rgb(r, g, b), colour => colour, } } /// Converts the colour into sRGB. /// /// Named colours (`Black`, `Red` etc. through `White`) are treated like /// `Fixed` colours with indexes 0 through 7. `Fixed` colours are converted /// into sRGB using [`rgb_from_ansi256`] function. `RGB` colours are /// returned unchanged. /// /// This implementation is present only if `ansi_term` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use ansi_term::Colour; /// /// assert_eq!(( 0, 0, 0), Colour::Fixed( 16).to_rgb()); /// assert_eq!(( 95, 135, 175), Colour::Fixed( 67).to_rgb()); /// assert_eq!((255, 255, 255), Colour::Fixed(231).to_rgb()); /// assert_eq!((238, 238, 238), Colour::Fixed(255).to_rgb()); /// assert_eq!(( 42, 24, 0), Colour::RGB(42, 24, 0).to_rgb()); /// ``` #[inline] fn to_rgb(&self) -> (u8, u8, u8) { let idx = match *self { Self::Black => 0, Self::Red => 1, Self::Green => 2, Self::Yellow => 3, Self::Blue => 4, Self::Purple => 5, Self::Cyan => 6, Self::White => 7, Self::Fixed(idx) => idx, Self::RGB(r, g, b) => return (r, g, b), }; rgb_from_ansi256(idx) } } #[cfg(feature = "termcolor")] impl AsRGB for termcolor::Color { /// Returns sRGB colour corresponding to escape code represented by /// [`termcolor::Color`]. /// /// Behaves slightly differently depending on the variant of the enum. /// - For named colour variants (`Black`, `Red` etc. up till `White`), /// returns corresponding system colour with indexes going from 0 to 7. /// - For `Ansi256` variant returns colour corresponding to specified index. /// See [`rgb_from_ansi256`](`rgb_from_ansi256`). /// - For `Rgb` variant converts it to 24-bit `0xRRGGBB` representation. /// /// This implementation is present only if `termcolor` crate feature is /// enabled. #[inline] fn as_u32(&self) -> u32 { match *self { Self::Black => ansi256::ANSI_COLOURS[0], Self::Blue => ansi256::ANSI_COLOURS[4], Self::Green => ansi256::ANSI_COLOURS[2], Self::Red => ansi256::ANSI_COLOURS[1], Self::Cyan => ansi256::ANSI_COLOURS[6], Self::Magenta => ansi256::ANSI_COLOURS[5], Self::Yellow => ansi256::ANSI_COLOURS[3], Self::White => ansi256::ANSI_COLOURS[7], Self::Ansi256(idx) => ansi256::ANSI_COLOURS[usize::from(idx)], Self::Rgb(r, g, b) => (r, g, b).as_u32(), _ => unreachable!(), } } /// Returns index of a colour in 256-colour ANSI palette approximating given /// sRGB colour. /// /// Behaves slightly differently depending on the variant of the enum. /// - For named colour variants (`Black`, `Red` etc. up till `White`), /// returns index going from 0 to 7. /// - For `Ansi256` variant simply returns index encoded in the variant. /// - Lastly, for `Rgb` variant, approximates the colour and returns index /// of closest colour in 256-colour palette. /// /// This implementation is present only if `termcolor` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::AsRGB; /// /// assert_eq!( 0, termcolor::Color::Black.to_ansi256()); /// assert_eq!( 7, termcolor::Color::White.to_ansi256()); /// assert_eq!( 42, termcolor::Color::Ansi256(42).to_ansi256()); /// assert_eq!( 16, termcolor::Color::Rgb( 0, 0, 0).to_ansi256()); /// assert_eq!( 16, termcolor::Color::Rgb( 1, 1, 1).to_ansi256()); /// assert_eq!( 16, termcolor::Color::Rgb( 0, 1, 2).to_ansi256()); /// assert_eq!( 67, termcolor::Color::Rgb( 95, 135, 175).to_ansi256()); /// assert_eq!(231, termcolor::Color::Rgb(255, 255, 255).to_ansi256()); /// ``` #[inline] fn to_ansi256(&self) -> u8 { match *self { Self::Black => 0, Self::Blue => 4, Self::Green => 2, Self::Red => 1, Self::Cyan => 6, Self::Magenta => 5, Self::Yellow => 3, Self::White => 7, Self::Ansi256(idx) => idx, Self::Rgb(r, g, b) => (r, g, b).to_ansi256(), _ => unreachable!(), } } } #[cfg(feature = "termcolor")] impl super::ColourExt for termcolor::Color { /// Constructs a `Ansi256` colour which approximates given sRGB colour. /// /// This implementation is present only if `termcolor` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use termcolor::Color; /// /// assert_eq!(Color::Ansi256( 16), Color::approx_rgb( 0, 0, 0)); /// assert_eq!(Color::Ansi256( 16), Color::approx_rgb( 0, 1, 2)); /// assert_eq!(Color::Ansi256( 67), Color::approx_rgb( 95, 135, 175)); /// assert_eq!(Color::Ansi256(231), Color::approx_rgb(255, 255, 255)); /// ``` #[inline] fn approx_rgb(r: u8, g: u8, b: u8) -> Self { Self::Ansi256(ansi256_from_rgb((r, g, b))) } /// Converts the colour into 256-colour-compatible format. /// /// If the colour represents an RGB colour, converts it into an `Ansi256` /// variant using [`ansi256_from_rgb`] function. Otherwise, returns the /// colour unchanged. /// /// This implementation is present only if `termcolor` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use termcolor::Color; /// /// assert_eq!(Color::Red, Color::Red.to_256()); /// assert_eq!(Color::Ansi256( 11), Color::Ansi256(11).to_256()); /// assert_eq!(Color::Ansi256( 16), Color::Rgb( 0, 0, 0).to_256()); /// assert_eq!(Color::Ansi256( 16), Color::Rgb( 0, 1, 2).to_256()); /// assert_eq!(Color::Ansi256( 67), Color::Rgb( 95, 135, 175).to_256()); /// assert_eq!(Color::Ansi256(231), Color::Rgb(255, 255, 255).to_256()); /// ``` #[inline] fn to_256(&self) -> Self { match *self { Self::Rgb(r, g, b) => Self::approx_rgb(r, g, b), colour => colour, } } /// Converts the colour into sRGB. /// /// Named colours (`Black`, `Red` etc. through `White`) are treated like /// `Ansi256` colours with indexes 0 through 7. `Ansi256` colours are /// converted into sRGB using [`rgb_from_ansi256`] function. `Rgb` colours /// are returned unchanged. /// /// This implementation is present only if `termcolor` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use termcolor::Color; /// /// assert_eq!(( 0, 0, 0), Color::Ansi256( 16).to_rgb()); /// assert_eq!(( 95, 135, 175), Color::Ansi256( 67).to_rgb()); /// assert_eq!((255, 255, 255), Color::Ansi256(231).to_rgb()); /// assert_eq!((238, 238, 238), Color::Ansi256(255).to_rgb()); /// assert_eq!(( 42, 24, 0), Color::Rgb(42, 24, 0).to_rgb()); /// ``` #[inline] fn to_rgb(&self) -> (u8, u8, u8) { let idx = match *self { Self::Black => 0, Self::Blue => 4, Self::Green => 2, Self::Red => 1, Self::Cyan => 6, Self::Magenta => 5, Self::Yellow => 3, Self::White => 7, Self::Ansi256(idx) => idx, Self::Rgb(r, g, b) => return (r, g, b), _ => unreachable!(), }; rgb_from_ansi256(idx) } } #[cfg(feature = "anstyle")] impl AsRGB for anstyle::RgbColor { /// Returns representation of the sRGB colour as a 24-bit `0xRRGGBB` /// integer. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. #[inline(always)] fn as_u32(&self) -> u32 { to_u32(self.0, self.1, self.2) } } #[cfg(feature = "anstyle")] impl ColourExt for anstyle::Ansi256Color { /// Constructs an colour which best approximates given sRGB colour. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use anstyle::Ansi256Color; /// /// assert_eq!(Ansi256Color( 16), Ansi256Color::approx_rgb( 0, 0, 0)); /// assert_eq!(Ansi256Color( 16), Ansi256Color::approx_rgb( 0, 1, 2)); /// assert_eq!(Ansi256Color( 67), Ansi256Color::approx_rgb( 95, 135, 175)); /// assert_eq!(Ansi256Color(231), Ansi256Color::approx_rgb(255, 255, 255)); /// ``` #[inline(always)] fn approx_rgb(r: u8, g: u8, b: u8) -> Self { Self(ansi256_from_rgb((r, g, b))) } /// Returns `self`. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. #[inline(always)] fn to_256(&self) -> Self { *self } /// Converts the colour into sRGB. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use anstyle::Ansi256Color; /// /// assert_eq!(( 0, 0, 0), Ansi256Color( 16).to_rgb()); /// assert_eq!(( 95, 135, 175), Ansi256Color( 67).to_rgb()); /// assert_eq!((255, 255, 255), Ansi256Color(231).to_rgb()); /// assert_eq!((238, 238, 238), Ansi256Color(255).to_rgb()); /// ``` #[inline(always)] fn to_rgb(&self) -> (u8, u8, u8) { rgb_from_ansi256(self.0) } } #[cfg(feature = "anstyle")] impl ColourExt for anstyle::Color { /// Constructs an ANSI 256 colour which best approximates given sRGB colour. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use anstyle::{Ansi256Color, Color}; /// /// assert_eq!(Color::Ansi256(Ansi256Color( 16)), /// Color::approx_rgb( 0, 0, 0)); /// assert_eq!(Color::Ansi256(Ansi256Color( 16)), /// Color::approx_rgb( 0, 1, 2)); /// assert_eq!(Color::Ansi256(Ansi256Color( 67)), /// Color::approx_rgb( 95, 135, 175)); /// assert_eq!(Color::Ansi256(Ansi256Color(231)), /// Color::approx_rgb(255, 255, 255)); /// ``` #[inline] fn approx_rgb(r: u8, g: u8, b: u8) -> Self { Self::Ansi256(anstyle::Ansi256Color::approx_rgb(r, g, b)) } /// Converts the colour into 256-colour-compatible format. /// /// If the colour represents an RGB colour, converts it into an `Ansi256` /// variant using [`ansi256_from_rgb`] function. Otherwise, returns the /// colour unchanged. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. /// /// # Examples /// /// ```ignore /// use ansi_colours::ColourExt; /// use anstyle::{Ansi256Color, AnsiColor, Color, RgbColor}; /// /// assert_eq!(Color::Ansi(AnsiColor::Red), /// Color::Ansi(AnsiColor::Red).to_256()); /// assert_eq!(Color::Ansi256(Ansi256Color( 11)), /// Color::Ansi256(Ansi256Color( 11)).to_256()); /// assert_eq!(Color::Ansi256(Ansi256Color( 16)), /// Color::Rgb(RgbColor( 0, 0, 0)).to_256()); /// assert_eq!(Color::Ansi256(Ansi256Color( 16)), /// Color::Rgb(RgbColor( 0, 1, 2)).to_256()); /// assert_eq!(Color::Ansi256(Ansi256Color( 67)), /// Color::Rgb(RgbColor( 95, 135, 175)).to_256()); /// assert_eq!(Color::Ansi256(Ansi256Color(231)), /// Color::Rgb(RgbColor(255, 255, 255)).to_256()); /// ``` #[inline] fn to_256(&self) -> Self { match *self { Self::Rgb(anstyle::RgbColor(r, g, b)) => Self::approx_rgb(r, g, b), colour => colour, } } /// Converts the colour into sRGB. /// /// `AnsiColour` and `Ansi256Color` colour variants are converted into sRGB /// using [`rgb_from_ansi256`] function. `Rgb` colours are returned /// unchanged. /// /// This implementation is present only if `anstyle` crate feature is /// enabled. /// /// # Examples /// /// ``` /// use ansi_colours::ColourExt; /// use anstyle::{Ansi256Color, AnsiColor, Color, RgbColor}; /// /// assert_eq!(( 0, 0, 0), Color::Ansi256(Ansi256Color( 16)).to_rgb()); /// assert_eq!(( 95, 135, 175), Color::Ansi256(Ansi256Color( 67)).to_rgb()); /// assert_eq!((255, 255, 255), Color::Ansi256(Ansi256Color(231)).to_rgb()); /// assert_eq!((238, 238, 238), Color::Ansi256(Ansi256Color(255)).to_rgb()); /// assert_eq!(( 42, 24, 0), Color::Rgb(RgbColor(42, 24, 0)).to_rgb()); /// ``` #[inline(always)] fn to_rgb(&self) -> (u8, u8, u8) { rgb_from_ansi256(match *self { Self::Ansi(colour) => colour as u8, Self::Ansi256(colour) => colour.0, Self::Rgb(anstyle::RgbColor(r, g, b)) => return (r, g, b), }) } } ansi_colours-1.2.3/src/lib.rs000064400000000000000000000263411046102023000142400ustar 00000000000000// ansi_colours – true-colour ↔ ANSI terminal palette converter // Copyright 2018 by Michał Nazarewicz // // ansi_colours is free software: you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation; either version 3 of the License, or (at // your option) any later version. // // ansi_colours is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser // General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with ansi_colours. If not, see . //! `ansi_colours` converts between 24-bit sRGB colours and 8-bit colour palette //! used by ANSI terminals such as xterm on rxvt-unicode in 256-colour mode. //! The most common use case is when using 24-bit colours in a terminal emulator //! which only support 8-bit colour palette. It allows true-colours to be //! approximated by values supported by the terminal. //! //! When mapping true-colour into available 256-colour palette, it tries to //! balance accuracy and performance. It doesn’t implement the fastest //! algorithm nor is it the most accurate, instead it uses a formula which //! should be fast enough and accurate enough for most use-cases. //! //! ## Cargo features //! //! To facilitate better interoperability the crate defines `rgb` crate //! feature (enabled by default). It adds support for the `RGB` type from //! [`rgb` crate](https://crates.io/crates/rgb). Specifically, `RGB8` //! (a.k.a. `RGB`) as well as `RGB16` (a.k.a. `RGB`) types are //! supported. //! //! Furthermore, `ansi_term` and `termcolor` features are available. They //! add support for `Colour` type from [`ansi_term` //! crate](https://crates.io/crates/ansi_term) and `Color` type from //! [`termcolor` crate](https://crates.io/crates/termcolor) respectively. //! This includes support for calling `ansi256_from_rgb` with arguments of //! those types and implementation of `ColourExt` trait which extends the //! types with additional conversion methods. //! //! ## Usage //! //! Using this library with Cargo projects is as simple as adding a single //! dependency: //! //! ```toml //! [dependencies] //! ansi_colours = "1.1" //! ``` //! //! and then using one of the two functions that the library provides: //! //! ```rust //! use ansi_colours::*; //! //! fn main() { //! // Colour at given index: //! println!("{:-3}: {:?}", 50, rgb_from_ansi256(50)); //! //! // Approximate true-colour by colour in the palette: #![cfg_attr( feature = "rgb", doc = r#" let rgb = rgb::RGB8 { r: 100, g: 200, b: 150 };"# )] #![cfg_attr(not(feature = "rgb"), doc = r#" let rgb = (100, 200, 150);"#)] //! let index = ansi256_from_rgb(rgb); //! println!("{:?} ~ {:-3} {:?}", rgb, index, rgb_from_ansi256(index)); //! } //! ``` #![no_std] mod ansi256; mod impls; #[cfg(test)] mod test; /// Returns sRGB colour corresponding to the index in the 256-colour ANSI /// palette. /// /// The first 16 colours (so-called system colours) are not standardised and /// terminal emulators often allow them to be customised. Because of this, /// their value should not be relied upon. For system colours, this function /// returns default colours used by XTerm. /// /// Remaining 240 colours consist of a 6×6×6 colour cube and a 24-step greyscale /// ramp. Those are standardised and thus should be the same on every terminal /// which supports 256-colour colour palette. /// /// The palette can be viewed on [helpful /// chart](https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg). /// /// # Examples /// /// /// ``` /// assert_eq!(( 0, 0, 0), ansi_colours::rgb_from_ansi256( 16)); /// assert_eq!(( 95, 135, 175), ansi_colours::rgb_from_ansi256( 67)); /// assert_eq!((255, 255, 255), ansi_colours::rgb_from_ansi256(231)); /// assert_eq!((238, 238, 238), ansi_colours::rgb_from_ansi256(255)); #[cfg_attr( feature = "rgb", doc = r#" let rgb = rgb::RGB8::from(ansi_colours::rgb_from_ansi256(128)); assert_eq!(rgb::RGB8 { r: 175, g: 0, b: 215 }, rgb); let grey = rgb::alt::Gray::(128); assert_eq!(244, ansi_colours::ansi256_from_rgb(grey)); "# )] /// ``` #[inline] pub fn rgb_from_ansi256(idx: u8) -> (u8, u8, u8) { let rgb = ansi256::ANSI_COLOURS[idx as usize]; ((rgb >> 16) as u8, (rgb >> 8) as u8, rgb as u8) } /// Returns index of a colour in 256-colour ANSI palette approximating given /// sRGB colour. /// /// Because the first 16 colours of the palette are not standardised and usually /// user-configurable, the function usually ignores them. /// /// The first argument uses [`AsRGB`] trait so that the function can be called in /// multiple ways using different representations of RGB colours such as /// `0xRRGGBB` integer, `(r, g, b)` tuple or `[r, g, b]` array. Calling the /// function is equivalent to calling [`AsRGB::to_ansi256`] method. /// /// # Examples /// /// /// ``` /// assert_eq!( 16, ansi_colours::ansi256_from_rgb(0x000000)); /// assert_eq!( 16, ansi_colours::ansi256_from_rgb( ( 1, 1, 1))); /// assert_eq!( 16, ansi_colours::ansi256_from_rgb( [ 0, 1, 2])); /// assert_eq!( 67, ansi_colours::ansi256_from_rgb(&( 95, 135, 175))); /// assert_eq!(231, ansi_colours::ansi256_from_rgb(&[255, 255, 255])); #[cfg_attr( feature = "rgb", doc = r#" let rgb = rgb::RGB8 { r: 175, g: 0, b: 215 }; assert_eq!(128, ansi_colours::ansi256_from_rgb(rgb)); let bgr = rgb::RGB8 { b: 215, g: 0, r: 175 }; assert_eq!(128, ansi_colours::ansi256_from_rgb(bgr)); let grey = rgb::alt::Gray::(128); assert_eq!(244, ansi_colours::ansi256_from_rgb(grey)); "# )] /// ``` #[inline] pub fn ansi256_from_rgb(rgb: C) -> u8 { rgb.to_ansi256() } /// Returns index of a colour in 256-colour ANSI palette approximating given /// shade of grey. /// /// This gives the same results as `ansi256_from_rgb((component, component, /// component))` but is faster. Provided that the `rgb` crate feature is /// enabled, it is equivalent (in behaviour and performance) to /// `ansi256_from_rgb(rgb::alt::Grey(component))`. /// /// # Examples /// /// /// ``` /// assert_eq!( 16, ansi_colours::ansi256_from_grey(0)); /// assert_eq!( 16, ansi_colours::ansi256_from_grey(1)); /// assert_eq!(231, ansi_colours::ansi256_from_grey(255)); #[cfg_attr( feature = "rgb", doc = r#" let grey = rgb::alt::Gray::(128); assert_eq!(244, ansi_colours::ansi256_from_grey(*grey)); assert_eq!(244, ansi_colours::ansi256_from_rgb(grey)); "# )] /// ``` #[inline] pub fn ansi256_from_grey(component: u8) -> u8 { ansi256::ANSI256_FROM_GREY[component as usize] } /// Type which represents a colour convertible to sRGB. Used to provide /// overloaded versions of `ansi256_from_rgb` function. pub trait AsRGB { /// Returns representation of the sRGB colour as a 24-bit `0xRRGGBB` /// integer. fn as_u32(&self) -> u32; /// Returns index of a colour in 256-colour ANSI palette approximating given /// sRGB colour. /// /// This is provided by default and uses [`Self::as_u32`] to determine /// 24-bit sRGB representation of the colour. /// /// An implementation should provide its own definition if it can offer /// a more direct approximation. For example, if `Self` represents shades /// of grey, it’s faster to use [`ansi256_from_grey`] than relay on `to_u32` /// conversion; or if it represents a variant which can store index in the /// palette or an RGB colour, it’s better to either return the index or /// perform approximation depending on the variant. #[inline] fn to_ansi256(&self) -> u8 { crate::ansi256::ansi256_from_rgb(self.as_u32()) } } /// Extension to types representing ANSI colours adding methods converting /// between RGB and indexed (a.k.a. fixed) representations. pub trait ColourExt: Sized { /// Constructs an indexed colour which approximates given sRGB colour. /// /// # Examples /// #[cfg_attr(feature = "ansi_term", doc = "```")] #[cfg_attr(not(feature = "ansi_term"), doc = "```ignore")] /// use ansi_colours::ColourExt; /// use ansi_term::Colour; /// /// assert_eq!(Colour::Fixed( 16), Colour::approx_rgb( 0, 0, 0)); /// assert_eq!(Colour::Fixed( 16), Colour::approx_rgb( 0, 1, 2)); /// assert_eq!(Colour::Fixed( 67), Colour::approx_rgb( 95, 135, 175)); /// assert_eq!(Colour::Fixed(231), Colour::approx_rgb(255, 255, 255)); /// ``` /// /// Note that the example requires `ansi_term` cargo feature to be enabled. fn approx_rgb(r: u8, g: u8, b: u8) -> Self; /// Constructs an indexed colour which approximates given sRGB colour. /// /// Behaves like [`approx_rgb`](`Self::approx_rgb`) but takes a single /// argument which implements [`AsRGB`]. Note that types which implement /// `ColourExt` typically also implement `AsRGB` which means this method can /// be called with `Self` argument. It’s usually better to call /// [`to_256`](`ColourExt::to_256`) instead. #[inline] fn approx(rgb: C) -> Self { let rgb = rgb.as_u32(); Self::approx_rgb((rgb >> 16) as u8, (rgb >> 8) as u8, rgb as u8) } /// Converts the colour into 256-colour-compatible format. /// /// If the colour represents an RGB colour, converts it into indexed /// representation using [`ansi256_from_rgb`] function. Otherwise, returns /// the colour unchanged. /// /// # Examples /// #[cfg_attr(feature = "ansi_term", doc = "```")] #[cfg_attr(not(feature = "ansi_term"), doc = "```ignore")] /// use ansi_colours::ColourExt; /// use ansi_term::Colour; /// /// assert_eq!(Colour::Red, Colour::Red.to_256()); /// assert_eq!(Colour::Fixed( 11), Colour::Fixed(11).to_256()); /// assert_eq!(Colour::Fixed( 16), Colour::RGB( 0, 0, 0).to_256()); /// assert_eq!(Colour::Fixed( 16), Colour::RGB( 0, 1, 2).to_256()); /// assert_eq!(Colour::Fixed( 67), Colour::RGB( 95, 135, 175).to_256()); /// assert_eq!(Colour::Fixed(231), Colour::RGB(255, 255, 255).to_256()); /// ``` /// /// Note that the example requires `ansi_term` cargo feature to be enabled. fn to_256(&self) -> Self; /// Converts the colour colour into sRGB. /// /// Named colours (black, red etc. through white) are treated like indexed /// colours with indexes 0 through 7. Indexed colours are converted into /// sRGB using [`rgb_from_ansi256`] function. RGB colours are returned /// unchanged. /// /// # Examples /// #[cfg_attr(feature = "ansi_term", doc = "```")] #[cfg_attr(not(feature = "ansi_term"), doc = "```ignore")] /// use ansi_colours::ColourExt; /// use ansi_term::Colour; /// /// assert_eq!(( 0, 0, 0), Colour::Fixed( 16).to_rgb()); /// assert_eq!(( 95, 135, 175), Colour::Fixed( 67).to_rgb()); /// assert_eq!((255, 255, 255), Colour::Fixed(231).to_rgb()); /// assert_eq!((238, 238, 238), Colour::Fixed(255).to_rgb()); /// assert_eq!(( 42, 24, 0), Colour::RGB(42, 24, 0).to_rgb()); /// ``` /// /// Note that the example requires `ansi_term` cargo feature to be enabled. fn to_rgb(&self) -> (u8, u8, u8); } ansi_colours-1.2.3/src/test.rs000064400000000000000000000071011046102023000144420ustar 00000000000000fn to_rgb(index: u8) -> (u8, u8, u8) { crate::rgb_from_ansi256(index) } fn to_ansi(rgb: (u8, u8, u8)) -> u8 { crate::ansi256_from_rgb(rgb) } static CUBE_VALUES: [u8; 6] = [0, 95, 135, 175, 215, 255]; /// Tests that getting colour from the ANSI palette gives desired result. #[test] fn test_to_rgb() { #[rustfmt::skip] static SYSTEM_COLOURS: [(u8, u8, u8); 16] = [ ( 0, 0, 0), (205, 0, 0), ( 0, 205, 0), (205, 205, 0), ( 0, 0, 238), (205, 0, 205), ( 0, 205, 205), (229, 229, 229), (127, 127, 127), (255, 0, 0), ( 0, 255, 0), (255, 255, 0), ( 92, 92, 255), (255, 0, 255), ( 0, 255, 255), (255, 255, 255), ]; // System colours for (idx, rgb) in SYSTEM_COLOURS.iter().enumerate() { assert_eq!(*rgb, to_rgb(idx as u8)); } // Colour cube for idx in 0..216 { assert_eq!( ( CUBE_VALUES[idx / 36], CUBE_VALUES[(idx / 6) % 6], CUBE_VALUES[idx % 6] ), to_rgb(16 + idx as u8) ); } // Greyscale ramp for idx in 0..24 { let y = idx * 10 + 8; assert_eq!((y, y, y), to_rgb(idx + 232)); } } /// Tries all colours in the 256-colour ANSI palette and chooses one with /// smallest ΔE*₀₀ to `rgb(y, y, y)`. fn best_grey(y: u8) -> u8 { let reference = empfindung::ToLab::to_lab(&rgb::alt::Gray(y)); CUBE_VALUES .iter() .enumerate() .map(|(idx, v)| (idx as u8 * (36 + 6 + 1) + 16, *v)) .chain((0..24u8).map(|idx| (idx + 232, idx * 10 + 8))) .map(|(idx, grey)| (idx, rgb::alt::Gray(grey))) .map(|(idx, grey)| (empfindung::cie00::diff(reference, grey), idx)) .reduce(|x, y| if x.0 < y.0 { x } else { y }) .unwrap() .1 } /// Tests that converting `(c, c, c)` colour gives the best possible result. #[test] fn test_from_rgb_grey() { for i in 0..256 { assert_eq!(best_grey(i as u8), to_ansi((i as u8, i as u8, i as u8))); } } /// Tests that getting value for grey colour given as RGB triple and one given /// as just shade of grey produce the same result. #[test] fn test_greys_agree() { for i in 0..256 { assert_eq!( to_ansi((i as u8, i as u8, i as u8)), crate::ansi256_from_grey(i as u8) ); } } /// Tests that converting colour which exists in the palette gives index of that /// colour in the palette. #[test] fn test_to_ansi_exact() { for i in 16..256 { let rgb = to_rgb(i as u8); let got = to_ansi(rgb); assert_eq!(i as u8, got, "want {:?} but got {:?}", rgb, to_rgb(got)); } } /// Tests a few approximations. #[test] #[rustfmt::skip] fn test_to_ansi_approx() { assert_eq!( 16, to_ansi(( 1, 1, 1))); assert_eq!(232, to_ansi(( 7, 7, 7))); assert_eq!(232, to_ansi(( 8, 7, 8))); assert_eq!( 64, to_ansi(( 97, 134, 8))); } /// Calculates RGB→ANSI for all colours and calculates a checksum of them /// comparing it to known value. This is meant to see whether refactoring of /// the code does not change the behaviour. If the computation is changed on /// purpose simply update the checksum in this test. #[test] #[cfg_attr(miri, ignore = "runs too slow on Miri")] fn from_rgb_checksum() { let mut buf = [0; 1 << 12]; let mut checksum = 0; for rgb in 0..(1 << 24) { buf[rgb % buf.len()] = crate::ansi256_from_rgb(rgb as u32); if rgb % buf.len() == buf.len() - 1 { checksum = crc64::crc64(checksum, &buf); } } assert_eq!(3373856917329536106, checksum); }