简体   繁体   English

Android将十六进制颜色用于Google Maps标记

[英]Android use HEX color for google maps marker

I want to use a custom hex color for google maps markers, but I saw you can only use hue colors from 0-360 and the 10 predefined ones. 我想为Google地图标记使用自定义的十六进制颜色,但是我看到您只能使用0-360和10种预定义的颜色。 Is there any way you can change the marker color to be a hex one, or at least convert the hex value to hue so I can use that instead? 有什么方法可以将标记颜色更改为十六进制颜色,或者至少将十六进制值转换为色调,以便我可以使用它呢? I already know how to set a hue color to the marker, but not a hex one. 我已经知道如何为标记设置色调颜色,但是不为十六进制设置颜色。

The following code shows an example. 以下代码显示了一个示例。 It's based on this great answer that uses JavaScript: https://stackoverflow.com/a/3732187/1207156 它基于使用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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM