【问题标题】:How to convert 3-digit HTML hex colors to 6-digit flex hex colors如何将 3 位 HTML 十六进制颜色转换为 6 位 flex 十六进制颜色
【发布时间】:2012-05-01 02:24:56
【问题描述】:

我想将来自 HTML CSS 的三位十六进制颜色转换为 Flex 的六位十六进制颜色。谁能给我代码将 3 位十六进制颜色转换为 6 位等效颜色?

【问题讨论】:

标签: apache-flex colors hex


【解决方案1】:

通过将每个数字加倍来扩展三位十六进制颜色(请参阅w3 spec)。 所以#F3A 扩展为#FF33AA

【讨论】:

    【解决方案2】:

    两位数:例如#A21 等于#AA2211

    但是这个问题是重复的:convert to 3-digit hex color code

    【讨论】:

      【解决方案3】:

      其他答案提供了流程,但我将使用正则表达式和 java 编程语言提供代码

      String value = "#FFF";
      value = value.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");
      

      【讨论】:

      • 感谢为 Android/Java 工作
      【解决方案4】:

      如果您来到这里并使用 Python,请按以下步骤操作:

      hex_code = '#FFF'
      new_hex_code = '#{}'.format(''.join(2 * c for c in hex_code.lstrip('#')))
      

      【讨论】:

        【解决方案5】:

        Ovokerie 答案的 Kotlin 版本:

        shortHexString.replace(Regex("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])"), "#$1$1$2$2$3$3")
        

        【讨论】:

          【解决方案6】:

          PHP 版本:

            /**
           * returns a clean 6 digit hex number as a string
           * @param $hex
           * @return false|string
           */
          function clean_hex_color($hex)
          {
              $hex = strtolower($hex);
              //remove the leading "#"
              if (strlen($hex) == 7 || strlen($hex) == 4)
                  $hex = substr($hex, -(strlen($hex) - 1));
          
              // $hex like "1a7"
              if (preg_match('/^[a-f0-9]{6}$/i', $hex))
                  return $hex;
              // $hex like "162a7b"
              elseif (preg_match('/^[a-f0-9]{3}$/i', $hex))
                  return $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
              //any other format
              else
                  return "000000";
          }
          

          【讨论】:

            猜你喜欢
            • 2020-12-25
            • 2015-05-19
            • 2010-11-30
            • 2019-10-29
            • 2011-07-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多