从十六进制颜色代码中查找颜色名称

2023-11-23

我想从十六进制颜色代码中找到颜色的名称。当我得到十六进制颜色代码时,我想找到最匹配的颜色名称。例如,对于代码 #c06040 ,如何确定它是棕色、蓝色还是黄色?这样我就可以在无需人工干预的情况下找到图像中物体的颜色。

颜色深浅的​​十六进制代码之间有什么关系吗? 如果有的话请给出一些示例代码。


有一个程序随 Python 源代码一起分发,称为pynche可以做到这一点

您需要的功能在方法中ColorDB.nearest() in ColorDB.py

来自Python/工具/自述文件

Introduction

    Pynche is a color editor based largely on a similar program that I
    originally wrote back in 1987 for the Sunview window system.  That
    editor was called ICE, the Interactive Color Editor.  I'd always
    wanted to port this program to X but didn't feel like hacking X
    and C code to do it.  Fast forward many years, to where Python +
    Tkinter provides such a nice programming environment, with enough
    power, that I finally buckled down and re-implemented it.  I
    changed the name because these days, too many other systems have
    the acronym `ICE'.

    Pynche should work with any variant of Python after 1.5.2
    (e.g. 2.0.1 and 2.1.1), using Tk 8.0.x.  It's been tested on
    Solaris 2.6, Windows NT 4, and various Linux distros.  You'll want
    to be sure to have at least Tk 8.0.3 for Windows.  Also, Pynche is
    very colormap intensive, so it doesn't work very well on 8-bit
    graphics cards; 24bit+ graphics cards are so cheap these days,
    I'll probably never "fix" that.

    Pynche must find a text database of colors names in order to
    provide `nearest' color matching.  Pynche is distributed with an
    rgb.txt file from the X11R6.4 distribution for this reason, along
    with other "Web related" database (see below).  You can use a
    different file with the -d option.  The file xlicense.txt contains
    the license only for rgb.txt and both files are in the X/
    subdirectory.

    Pynche is pronounced: Pin'-chee


Running Standalone

    On Unix, start it by running the `pynche' script.  On Windows, run
    pynche.pyw to inhibit the console window.  When run from the
    command line, the following options are recognized:

    --database file
    -d file
        Alternate location of the color database file.  Without this
        option, the first valid file found will be used (see below).

    --initfile file
    -i file
        Alternate location of the persistent initialization file.  See 
        the section on Persistency below.

    --ignore
    -X
        Ignore the persistent initialization file when starting up.
        Pynche will still write the current option settings to the
        persistent init file when it quits.

    --help
    -h
        Print the help message.

    initialcolor
        a Tk color name or #rrggbb color spec to be used as the
        initially selected color.  This overrides any color saved in
        the persistent init file.  Since `#' needs to be escaped in
        many shells, it is optional in the spec (e.g. #45dd1f is the
        same as 45dd1f).


Running as a Modal Dialog

    Pynche can be run as a modal dialog, inside another application,
    say as a general color chooser.  In fact, Grail 0.6 uses Pynche
    and a future version of IDLE may as well.  Pynche supports the API
    implemented by the Tkinter standard tkColorChooser module, with a
    few changes as described below.  By importing pyColorChooser from
    the Pynche package, you can run

        pyColorChooser.askcolor()

    which will popup Pynche as a modal dialog, and return the selected 
    color.

    There are some UI differences when running as a modal
    vs. standalone.  When running as a modal, there is no "Quit" menu
    item under the "File" menu.  Instead there are "Okay" and "Cancel"
    buttons.

    When "Okay" is hit, askcolor() returns the tuple

        ((r, g, b), "name")

    where r, g, and b are red, green, and blue color values
    respectively (in the range 0 to 255).  "name" will be a color name
    from the color database if there is an exact match, otherwise it
    will be an X11 color spec of the form "#rrggbb".  Note that this
    is different than tkColorChooser, which doesn't know anything
    about color names.

    askcolor() supports the following optional keyword arguments:

        color
            the color to set as the initial selected color

        master[*]
            the master window to use as the parent of the modal
            dialog.  Without this argument, pyColorChooser will create 
            its own Tkinter.Tk instance as the master.  This may not
            be what you want.

        databasefile
            similar to the --database option, the value must be a
            file name

        initfile[*]
            similar to the --initfile option, the value must be a
            file name

        ignore[*]
            similar to the --ignore flag, the value is a boolean

        wantspec
            When this is true, the "name" field in the return tuple
            will always be a color spec of the form "#rrggbb".  It
            will not return a color name even if there is a match;
            this is so pyColorChooser can exactly match the API of
            tkColorChooser.

        [*] these arguments must be specified the first time
        askcolor() is used and cannot be changed on subsequent calls.


The Colorstrip Window

    The top part of the main Pynche window contains the "variation
    strips".  Each strip contains a number of "color chips".  The
    strips always indicate the currently selected color by a highlight
    rectangle around the selected color chip, with an arrow pointing
    to the chip.  Each arrow has an associated number giving you the
    color value along the variation's axis.  Each variation strip
    shows you the colors that are reachable from the selected color by
    varying just one axis of the color solid.

    For example, when the selected color is (in Red/Green/Blue
    notation) 127/127/127, the Red Variations strip shows you every
    color in the range 0/127/127 to 255/127/127.  Similarly for the
    green and blue axes.  You can select any color by clicking on its
    chip.  This will update the highlight rectangle and the arrow, as
    well as other displays in Pynche.

    Click on "Update while dragging" if you want Pynche to update the
    selected color while you drag along any variation strip (this will
    be a bit slower).  Click on "Hexadecimal" to display the arrow
    numbers in hex.

    There are also two shortcut buttons in this window, which
    auto-select Black (0/0/0) and White (255/255/255).


The Proof Window

    In the lower left corner of the main window you see two larger
    color chips.  The Selected chip shows you a larger version of the
    color selected in the variation strips, along with its X11 color
    specification.  The Nearest chip shows you the closest color in
    the X11 database to the selected color, giving its X11 color
    specification, and below that, its X11 color name.  When the
    Selected chip color exactly matches the Nearest chip color, you
    will see the color name appear below the color specification for
    the Selected chip.

    Clicking on the Nearest color chip selects that color.  Color
    distance is calculated in the 3D space of the RGB color solid and
    if more than one color name is the same distance from the selected
    color, the first one found will be chosen.

    Note that there may be more than one X11 color name for the same
    RGB value.  In that case, the first one found in the text database
    is designated the "primary" name, and this is shown under the
    Nearest chip.  The other names are "aliases" and they are visible
    in the Color List Window (see below).

    Both the color specifications and color names are selectable for
    copying and pasting into another window.


The Type-in Window

    At the lower right of the main window are three entry fields.
    Here you can type numeric values for any of the three color axes.
    Legal values are between 0 and 255, and these fields do not allow
    you to enter illegal values.  You must hit Enter or Tab to select
    the new color.

    Click on "Update while typing" if you want Pynche to select the
    color on every keystroke (well, every one that produces a legal
    value!)  Click on "Hexadecimal" to display and enter color values
    in hex.


Other Views

    There are three secondary windows which are not displayed by
    default.  You can bring these up via the "View" menu on the main
    Pynche window.


The Text Window

    The "Text Window" allows you to see what effects various colors
    have on the standard Tk text widget elements.  In the upper part
    of the window is a plain Tk text widget and here you can edit the
    text, select a region of text, etc.  Below this is a button "Track
    color changes".  When this is turned on, any colors selected in
    the other windows will change the text widget element specified in
    the radio buttons below.  When this is turned off, text widget
    elements are not affected by color selection.

    You can choose which element gets changed by color selection by
    clicking on one of the radio buttons in the bottom part of this
    window.  Text foreground and background affect the text in the
    upper part of the window.  Selection foreground and background
    affect the colors of the primary selection which is what you see
    when you click the middle button (depending on window system) and
    drag it through some text.

    The Insertion is the insertion cursor in the text window, where
    new text will be inserted as you type.  The insertion cursor only
    has a background.


The Color List Window

    The "Color List" window shows every named color in the color name
    database (this window may take a while to come up).  In the upper
    part of the window you see a scrolling list of all the color names
    in the database, in alphabetical order.  Click on any color to
    select it.  In the bottom part of the window is displayed any
    aliases for the selected color (those color names that have the
    same RGB value, but were found later in the text database).  For
    example, find the color "Black" and you'll see that its aliases
    are "gray0" and "grey0".

    If the color has no aliases you'll see "<no aliases>" here.  If you
    just want to see if a color has an alias, and do not want to select a
    color when you click on it, turn off "Update on Click".

    Note that the color list is always updated when a color is selected
    from the main window.  There's no way to turn this feature off.  If
    the selected color has no matching color name you'll see
    "<no matching color>" in the Aliases window.


The Details Window

    The "Details" window gives you more control over color selection
    than just clicking on a color chip in the main window.  The row of
    buttons along the top apply the specified increment and decrement
    amounts to the selected color.  These delta amounts are applied to
    the variation strips specified by the check boxes labeled "Move
    Sliders".  Thus if just Red and Green are selected, hitting -10
    will subtract 10 from the color value along the red and green
    variation only.  Note the message under the checkboxes; this
    indicates the primary color level being changed when more than one
    slider is tied together.  For example, if Red and Green are
    selected, you will be changing the Yellow level of the selected
    color.

    The "At Boundary" behavior determines what happens when any color
    variation hits either the lower or upper boundaries (0 or 255) as
    a result of clicking on the top row buttons:

    Stop
        When the increment or decrement would send any of the tied
        variations out of bounds, the entire delta is discarded.

    Wrap Around
        When the increment or decrement would send any of the tied
        variations out of bounds, the out of bounds value is wrapped
        around to the other side.  Thus if red were at 238 and +25
        were clicked, red would have the value 7.

    Preserve Distance
        When the increment or decrement would send any of the tied
        variations out of bounds, all tied variations are wrapped as
        one, so as to preserve the distance between them.  Thus if
        green and blue were tied, and green was at 238 while blue was
        at 223, and +25 were clicked, green would be at 15 and blue
        would be at 0.

    Squash
        When the increment or decrement would send any of the tied
        variations out of bounds, the out of bounds variation is set
        to the ceiling of 255 or floor of 0, as appropriate.  In this
        way, all tied variations are squashed to one edge or the
        other.

    The top row buttons have the following keyboard accelerators:

    -25 == Shift Left Arrow
    -10 == Control Left Arrow
     -1 == Left Arrow
     +1 == Right Arrow
    +10 == Control Right Arrow
    +25 == Shift Right Arrow


Keyboard Accelerators

    Alt-w in any secondary window dismisses the window.  In the main
    window it exits Pynche (except when running as a modal).

    Alt-q in any window exits Pynche (except when running as a modal).


Persistency

    Pynche remembers various settings of options and colors between
    invocations, storing these values in a `persistent initialization
    file'.  The actual location of this file is specified by the
    --initfile option (see above), and defaults to ~/.pynche.

    When Pynche exits, it saves these values in the init file, and
    re-reads them when it starts up.  There is no locking on this
    file, so if you run multiple instances of Pynche at a time, you
    may clobber the init file.

    The actual options stored include

    - the currently selected color

    - all settings of checkbox and radio button options in all windows

    - the contents of the text window, the current text selection and
      insertion point, and all current text widget element color
      settings.

    - the name of the color database file (but not its contents)

    You can inhibit Pynche from reading the init file by supplying the
    --ignore option on the command line.  However, you cannot suppress
    the storing of the settings in the init file on Pynche exit.  If
    you really want to do this, use /dev/null as the init file, using
    --initfile.


Color Name Database Files

    Pynche uses a color name database file to calculate the nearest
    color to the selected color, and to display in the Color List
    view.  Several files are distributed with Pynche, described
    below.  By default, the X11 color name database file is selected.
    Other files:

    html40colors.txt -- the HTML 4.0 guaranteed color names

    websafe.txt -- the 216 "Web-safe" colors that Netscape and MSIE
    guarantee will not be dithered.  These are specified in #rrggbb
    format for both values and names

    webcolors.txt -- The 140 color names that Tim Peters and his
    sister say NS and MSIE both understand (with some controversy over 
    AliceBlue).

    namedcolors.txt -- an alternative set of Netscape colors.

    You can switch between files by choosing "Load palette..." from
    the "File" menu.  This brings up a standard Tk file dialog.
    Choose the file you want and then click "Ok".  If Pynche
    understands the format in this file, it will load the database and 
    update the appropriate windows.  If not, it will bring up an error 
    dialog.


To Do

    Here's a brief list of things I want to do (some mythical day):

    - Better support for resizing the top level windows

    - More output views, e.g. color solids

    - Have the notion of a `last color selected'; this may require a
      new output view

    - Support setting the font in the text view

    - Support distutils setup.py for installation

    I'm open to suggestions!




Local Variables:
indent-tabs-mode: nil
End:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从十六进制颜色代码中查找颜色名称 的相关文章

随机推荐

  • AVAudioRecorder 不会在设备上录制

    这是我的方法 void playOrRecord UIButton sender if playBool YES NSError error nil NSString filePath NSBundle mainBundle pathFor
  • 解决应用内购买缺少促销代码的问题

    Apple 不提供应用内购买的促销代码 让用户免费尝试通过应用内购买解锁的功能或内容 同时遵守 Apple 开发者指南的最佳方式是什么 这个想法是允许一组特殊的用户 评论者 主要粉丝等 无需付费即可访问作为应用内购买提供的内容或功能 解决此
  • 在 Android Activity 中设置全屏亮度

    我正在使用这种方法将屏幕设置为全亮度 SuppressLint NewApi private void setFullBright if Build VERSION SDK INT gt Build VERSION CODES CUPCAK
  • Rails 4:跳过回调

    我的应用程序中有一个拍卖和一个投标对象 当有人按下出价按钮然后它调用投标创建创建出价的控制器 然后对拍卖对象执行一些其他操作 投标控制器 gt 创建 auction endtime auction auctiontimer auction
  • NSMutableArray addObject 不起作用

    我已宣布NSMutableArray categories在我的视图控制器 h 文件中 并为其声明一个属性 In the parser foundCharacters 的方法NSXMLParser在我的 m 文件中 我有以下代码 void
  • 制作简单的 Google Android Maps API v2 项目时 Android Studio 中的编译错误

    最近我迁移到了新Android Studio 集成开发环境基于IntelliJ 我遵循的指南是 https developers google com maps documentation android start 为了 基本 如何在 A
  • Android In App Update 在立即模式下下载 APK 后不安装 APK

    应用程序正在使用立即模式来更新应用程序 它开始下载 APK 但下载 APK 后 它永远不会安装 UI 挂起并显示下载进度 100 立即模式下没有反馈下载已完成 即时模式如何实现app自动下载 安装 重启 我正在使用以下模式以立即模式开始更新
  • C - 可移植地获取类型对齐

    我正在为一种非常简单的语言编写非常小的解释器 它允许简单的结构定义 由其他结构和简单类型组成 如 int char float double 等 我希望字段使用尽可能少的对齐方式 因此使用 max align t 或类似的东西是不可能的 现
  • XHTML 和 &(与号)的编码

    我的网站符合 XHTML Transitional 标准除了一件事 URL 中的 与号 按原样编写 而不是 amp 也就是说 我的页面中的所有 URL 通常都是这样的 a href http www example org page asp
  • 最好使用游标适配器或数组适配器

    我在数据库中存储了大约 100 个时间表 需要根据每周 下周 下个月 逾期时间表等要求基于 Listview 显示它们 是否可以在应用程序启动时加载所有计划并根据用户在阵列适配器中选择的选项 每周 逾期 每月等 显示它们 或者在运行时使用查
  • 管理 .NET 应用程序在终止/终止时正常关闭

    我们有一个具有许多前台线程的 NET 控制台应用程序 如果我们使用任务管理器终止进程或从 Windows 命令行发出killjob kill 是否有一种方法可以优雅地关闭应用程序 在 net 控制台应用程序中添加托管代码 例如拥有一个函数被
  • ViewFlipper 与 Fragments

    我有一个带有 ViewFlipper 的 Activity 它可以在一堆视图 页面 和我的数据之间翻转 我正在考虑使用fragments API 在我的视图之间切换 这样做有什么好处呢 由于 ViewFlipper 本质上会切换可见性标志
  • 如何在Windows中最好地设置java的路径

    我很好奇我在 windows 环境中看到的 java 路径之间存在的差异 如果我执行一个命令where java我可以看到 C Program Files x86 Common Files Oracle Java javapath java
  • 检测用户触发的jquery事件或代码调用

    我有一些window onscroll event window scroll function e My Stuff 但在我的代码中我调用动画滚动到某个地方 html body stop animate scrollTop 555 100
  • 如何使用 jQuery 从元素获取边框半径?

    我有一个 div 包含以下 HTML 和 CSS 为了使我的 Javascript 代码更加健壮 我尝试从所选元素中检索某些 CSS 属性 我知道如何使用 css getter 来获取元素 但是如何使用该方法获取边框半径 jQuery 的文
  • iOS 8 TestFlight 不安装应用程序

    正如标题所说 我正在尝试预发布一个 ios8 应用程序 该应用程序已从存档成功上传并显示在预发布下 我按下提交测试版并添加内部测试用户 在 testflight 中 该应用程序出现 但是当我按安装时 它显示 无法安装应用程序 testfli
  • 使用 jQuery 以编程方式触发 IE 的 Javascript 事件

    当 IE 中的用户触发事件时 它被设置为window event目的 查看触发事件的唯一方法是访问window event对象 据我所知 如果以编程方式触发事件 例如通过 jQuery 触发事件 这会导致 ASP NET 验证器出现问题 在
  • Eclipse:打开类和对象的语法突出显示

    是否有可能像在 Visual Studio 中一样在 Eclipse 中打开类和对象的突出显示 目前仅突出显示变量名称 左 Eclipse 右 Visual Studio 2012 Image http image uploader de
  • 转换输入时“ValueError:无法将字符串转换为浮点数”

    最近我一直在编写代码 并在这个错误上陷入困境几天 基本上 该程序计算您每天必须摄入多少卡路里 我必须从条目中获取输入 但我不知道如何将该输入 默认情况下是字符串 转换为浮点数才能开始使用数字 我正在使用 Python 3 和 Tkinter
  • 从十六进制颜色代码中查找颜色名称

    我想从十六进制颜色代码中找到颜色的名称 当我得到十六进制颜色代码时 我想找到最匹配的颜色名称 例如 对于代码 c06040 如何确定它是棕色 蓝色还是黄色 这样我就可以在无需人工干预的情况下找到图像中物体的颜色 颜色深浅的 十六进制代码之间