首页 > 解决方案 > Google Slides API,可以使用十六进制设置颜色吗?

问题描述

是否可以使用十六进制格式设置元素形状背景颜色,例如 #B6D7A8 ?我在这里看到一个例子, 谷歌幻灯片 API-如何更改某种颜色的所有形状的文本颜色

但是,Google Slide API 参考并没有提到它。我想确定,并且专门针对 Java 环境。

https://developers.google.com/slides/reference/rest/v1/presentations.pages/other#Page.OpaqueColor

    requests.add(new Request()
            .setUpdateShapeProperties(new UpdateShapePropertiesRequest()
                .setObjectId(elementRandomString)
                    .setShapeProperties(new ShapeProperties()
                            .setShapeBackgroundFill(new ShapeBackgroundFill()
                                .setSolidFill(new SolidFill()
                                        .setColor(new Col)

在此处输入图像描述

标签: google-slides-apigoogle-slides

解决方案


Google Slides API-How to change text color for all shapes of a specific color 中提供的答案使用Apps Script 中的Slide Service来设置颜色。

要回答您的问题,无法以 HEX 格式设置颜色。如您提供的参考文档中所述,您需要在 OpaqueColor 中提供一个RgbColor对象。

解决方法:

  1. 将您的十六进制颜色转换为RGB然后除以值,red以获得RgbColorblue接受的值,即green2550.0 ~ 1.0

您可能想将此视为参考:java : convert Hex color #RRGGBB to rgb rgb?

  1. 根据您转换的,和值设置RgbColor 。redbluegreen

示例代码:

requests.add(new Request()
            .setUpdateShapeProperties(new UpdateShapePropertiesRequest()
                .setObjectId(elementRandomString)
                    .setShapeProperties(new ShapeProperties()
                            .setShapeBackgroundFill(new ShapeBackgroundFill()
                                .setSolidFill(new SolidFill()
                                        .setColor(new Color()
                                                .setRgbColor(new RgbColor()
                                                      .setRed(redFloatValue)
                                                      .setBlue(blueFloatValue)
                                                      .setGreen(greenFloatValue))))))));

参考:


推荐阅读