|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/// An aspect ratio for generated images. |
| 16 | +public struct AspectRatio: Sendable { |
| 17 | + /// The raw string value of the aspect ratio. |
| 18 | + let rawValue: String |
| 19 | + |
| 20 | + /// Creates a new aspect ratio with a raw string value. |
| 21 | + private init(rawValue: String) { |
| 22 | + self.rawValue = rawValue |
| 23 | + } |
| 24 | + |
| 25 | + /// Square (1:1) aspect ratio. |
| 26 | + public static let square1x1 = AspectRatio(rawValue: "1:1") |
| 27 | + |
| 28 | + /// Portrait (2:3) aspect ratio. |
| 29 | + public static let portrait2x3 = AspectRatio(rawValue: "2:3") |
| 30 | + |
| 31 | + /// Landscape (3:2) aspect ratio. |
| 32 | + public static let landscape3x2 = AspectRatio(rawValue: "3:2") |
| 33 | + |
| 34 | + /// Portrait (3:4) aspect ratio. |
| 35 | + public static let portrait3x4 = AspectRatio(rawValue: "3:4") |
| 36 | + |
| 37 | + /// Landscape (4:3) aspect ratio. |
| 38 | + public static let landscape4x3 = AspectRatio(rawValue: "4:3") |
| 39 | + |
| 40 | + /// Portrait (4:5) aspect ratio. |
| 41 | + public static let portrait4x5 = AspectRatio(rawValue: "4:5") |
| 42 | + |
| 43 | + /// Landscape (5:4) aspect ratio. |
| 44 | + public static let landscape5x4 = AspectRatio(rawValue: "5:4") |
| 45 | + |
| 46 | + /// Portrait (9:16) aspect ratio. |
| 47 | + public static let portrait9x16 = AspectRatio(rawValue: "9:16") |
| 48 | + |
| 49 | + /// Landscape (16:9) aspect ratio. |
| 50 | + public static let landscape16x9 = AspectRatio(rawValue: "16:9") |
| 51 | + |
| 52 | + /// Landscape (21:9) aspect ratio. |
| 53 | + public static let landscape21x9 = AspectRatio(rawValue: "21:9") |
| 54 | +} |
| 55 | + |
| 56 | +// MARK: - Codable Conformances |
| 57 | + |
| 58 | +extension AspectRatio: Encodable { |
| 59 | + public func encode(to encoder: Encoder) throws { |
| 60 | + var container = encoder.singleValueContainer() |
| 61 | + try container.encode(rawValue) |
| 62 | + } |
| 63 | +} |
0 commit comments