【问题标题】:Android use HEX color for google maps markerAndroid 使用 HEX 颜色作为谷歌地图标记
【发布时间】:2015-05-17 15:10:33
【问题描述】:

我想为谷歌地图标记使用自定义十六进制颜色,但我看到您只能使用 0-360 和 10 种预定义的色调颜色。有什么方法可以将标记颜色更改为十六进制颜色,或者至少将十六进制值转换为色调,以便我可以使用它?我已经知道如何为标记设置色调颜色,但不是十六进制颜色。

【问题讨论】:

  • 下面是一个例子:stackoverflow.com/a/3732187/1207156.
  • 答案是javascript,我不需要,但还是谢谢你
  • 好的,我认为您将其转换为 Java 会很容易。我为您进行了转换并将其发布在下面。如果您比较代码,您会发现它们非常相似。

标签: android google-maps colors marker


【解决方案1】:

以下代码显示了一个示例。它基于使用 JavaScript 的出色答案:https://stackoverflow.com/a/3732187/1207156

public class Convert {

    public static class Hsl {
        public double h, s, l;

        public Hsl(double h, double s, double l) {
            this.h = h;
            this.s = s;
            this.l = l;
        }
    }

    public static void main(String[] args) {
        String color = "#c7d92c"; // A nice shade of green.
        int r = Integer.parseInt(color.substring(1, 3), 16); // Grab the hex representation of red (chars 1-2) and convert to decimal (base 10).
        int g = Integer.parseInt(color.substring(3, 5), 16);
        int b = Integer.parseInt(color.substring(5, 7), 16);    

        double hue = rgbToHsl(r, g, b).h * 360;

        System.out.println("The hue value is " + hue);
    }

    private static Hsl rgbToHsl(double r, double g, double b) {
        r /= 255d; g /= 255d; b /= 255d;

        double max = Math.max(Math.max(r, g), b), min = Math.min(Math.min(r, g), b);
        double h, s, l = (max + min) / 2;

        if (max == min) {
            h = s = 0; // achromatic
        } else {
            double d = max - min;
            s = l > 0.5 ? d / (2 - max - min) : d / (max + min);

            if (max == r) h = (g - b) / d + (g < b ? 6 : 0);
            else if (max == g) h = (b - r) / d + 2;
            else h = (r - g) / d + 4; // if (max == b)

            h /= 6;
        }

        return new Hsl(h, s, l);
    }

}

【讨论】:

  • 是的,它们确实非常相似。感谢您为转换此脚本所做的努力:)
猜你喜欢
  • 2018-04-27
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
相关资源
最近更新 更多