hex转换rgb_深入JavaScript:如何创建Hex2RGB颜色转换器

hex转换rgb

Update (23/07/2019): I have corrected a few grammatical errors and changed app.js code a bit by removing the checkBG function.

更新(23/07/2019):我更正了一些语法错误,并通过删除checkBG函数对app.js代码进行了一些更改。

In this article, we’ll be creating a web-app that converts color codes between Hexadecimal form and RGB form.

在本文中,我们将创建一个Web应用程序,该应用程序可在十六进制形式和RGB形式之间转换颜色代码。

You can find a demo here and the source code here.

您可以在此处找到演示源代码

项目结构: (Project Structure:)

The project structure is pretty simple.

项目结构非常简单。

  1. index.html : Contains the structure of the app.

    index.html :包含应用程序的结构。

  2. style.css : Styles the page.

    style.css :设置页面样式。

  3. app.js : Contains all the magic code.

    app.js :包含所有魔术代码。

理念: (Idea:)

Here’s the list of things I wanted this app to perform:

以下是我希望该应用执行的操作的列表:

  1. Whenever something is typed in a text-field for hex, the app should check if the color is valid. If it is, convert it to RGB, set it as the background and then put the RGB value in the RGB text-field and vice versa.

    每当在文本字段中键入十六进制的内容时,应用程序应检查颜色是否有效。 如果是这样,请将其转换为RGB,将其设置为背景,然后将RGB值放入RGB文本字段中,反之亦然。
  2. If a short hex color code is typed into the text-field, expand it when the text-field loses focus (user clicks outside of text area).

    如果在文本字段中键入了一个短的十六进制颜色代码,则当文本字段失去焦点(用户在文本区域外单击)时,将其展开。
  3. Automatically prepend ‘#’ symbol to the hex input.

    将“#”符号自动添加到十六进制输入的前面。

让我们开始! (Let’s Begin!)

index.html (index.html)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Hex to RGB Converter</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="head">
    HEX &lt;--&gt; RGB
  </div>
  <div id="content">
    <input type="text" id="hex" placeholder="hex">
    <img id="hexError" class="hidden" src="data:image/svg+xml;utf8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1NzYgNTEyIj48cGF0aCBkPSJNNTY5LjUxNyA0NDAuMDEzQzU4Ny45NzUgNDcyLjAwNyA1NjQuODA2IDUxMiA1MjcuOTQgNTEySDQ4LjA1NGMtMzYuOTM3IDAtNTkuOTk5LTQwLjA1NS00MS41NzctNzEuOTg3TDI0Ni40MjMgMjMuOTg1YzE4LjQ2Ny0zMi4wMDkgNjQuNzItMzEuOTUxIDgzLjE1NCAwbDIzOS45NCA0MTYuMDI4ek0yODggMzU0Yy0yNS40MDUgMC00NiAyMC41OTUtNDYgNDZzMjAuNTk1IDQ2IDQ2IDQ2IDQ2LTIwLjU5NSA0Ni00Ni0yMC41OTUtNDYtNDYtNDZ6bS00My42NzMtMTY1LjM0Nmw3LjQxOCAxMzZjLjM0NyA2LjM2NCA1LjYwOSAxMS4zNDYgMTEuOTgyIDExLjM0Nmg0OC41NDZjNi4zNzMgMCAxMS42MzUtNC45ODI
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值