如何解决Google Slides API,能够使用十六进制来设置颜色吗?
是否可以使用十六进制格式设置元素形状背景颜色,例如 #B6D7A8 ?我在这里看到一个例子, Google Slides API- How to change text color for all shapes of a certain color
但是,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 API- How to change text color for all shapes of a certain color 中提供的答案使用 Apps 脚本中的 Slide Service 来设置颜色。
回答您的问题,无法以十六进制格式设置颜色。 如您提供的参考文档中所述,您需要在 RgbColor 中提供一个 OpaqueColor 对象。
解决方法:
- 将您的十六进制颜色转换为
RGB
,然后将red
、blue
和green
值除以255
以获得可接受的 RgbColor 值是0.0 ~ 1.0
您可能希望将此作为参考:java : convert Hex color #RRGGBB to rgb r g b?
- 根据转换后的
red
、blue
和green
值设置 RgbColor。
示例代码:
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))))))));