<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fabio Barbato on Medium]]></title>
        <description><![CDATA[Stories by Fabio Barbato on Medium]]></description>
        <link>https://medium.com/@FabioBarbato2?source=rss-d4d801e2cb47------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6ZvaBfHeTX7IgXsijQMKpw.jpeg</url>
            <title>Stories by Fabio Barbato on Medium</title>
            <link>https://medium.com/@FabioBarbato2?source=rss-d4d801e2cb47------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 01:54:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@FabioBarbato2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to generate color palette with The Color API in Swift]]></title>
            <link>https://medium.com/@FabioBarbato2/how-to-generate-color-palette-with-the-color-api-in-swift-3cde8b4874a?source=rss-d4d801e2cb47------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cde8b4874a</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[colors]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Fabio Barbato]]></dc:creator>
            <pubDate>Tue, 07 Feb 2023 09:30:28 GMT</pubDate>
            <atom:updated>2023-02-07T09:31:07.105Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Overview</strong></h3><p>The Color API is a web-based application programming interface (API) that provides color information and manipulation tools for developers. With this API, developers can retrieve information about colors and generate color palettes based on a single color, which can be useful for creating cohesive designs. The Color API is designed to be easy to use and accessible to developers of all skill levels.</p><h3>Create the struct</h3><p>First of all, you must create the struct to parse data with JSONDecoder. So, create a new Swift File and create the following struct:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd89999179150b63ebbb02650c25ba9f/href">https://medium.com/media/dd89999179150b63ebbb02650c25ba9f/href</a></iframe><h3>API call</h3><p>Once created the struct, create a new Swift File and define the new class to manage api calls. Here an example of class definition:</p><pre>import SwiftUI<br><br>class colorStructViewModel{<br>    var colorsToFetch : ColorStruct?<br>    let decoder = JSONDecoder()<br>    var urlComponents = URLComponents(string: &quot;https://www.thecolorapi.com&quot;)<br>}</pre><p>Then, define a function to call API and decode JSON file. Here an example of function that fetch a color palette:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed013ca1e79438bd8bf0f13f97a2eab9/href">https://medium.com/media/ed013ca1e79438bd8bf0f13f97a2eab9/href</a></iframe><p>The function is asynchronous and it takes in input three doubles to represents the rgb value of the given color. It gives in output an array of fetched colors.</p><p>Use the path <em>/scheme </em>to generate the color palette and query:</p><ul><li><em>rgb</em>: to put your rgb value given from the input;</li><li><em>mode</em>: define mode by which to generate the scheme from the input color (in this case <em>analogic-complement</em>);</li><li><em>count</em>: number of colors to return (two colors in this case);</li></ul><p>Assign the decoded JSON file to the variable <em>colorsToFetch </em>and then extract the generated colors. In this case, the hex value will be extracted. To convert the hex value to the rgb value, use the following private function:</p><pre>    private func hexToRGB(hex: String) -&gt; Color {<br>        var hex = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()<br>        if hex.count != 6 {<br>            return Color.white<br>        }<br>        var rgb: UInt64 = 0<br>        Scanner(string: hex).scanHexInt64(&amp;rgb)<br>        return Color(red: CGFloat((rgb &amp; 0xFF0000) &gt;&gt; 16) / 255.0,<br>                     green: CGFloat((rgb &amp; 0x00FF00) &gt;&gt; 8) / 255.0,<br>                     blue: CGFloat(rgb &amp; 0x0000FF) / 255.0)<br>    }</pre><p>Here the following code of entire class:</p><pre>import SwiftUI<br><br>class colorStructViewModel {<br>    <br>    var colorsToFetch : ColorStruct?<br>    let decoder = JSONDecoder()<br>    var urlComponents = URLComponents(string: &quot;https://www.thecolorapi.com&quot;)<br>    <br>    func matchColors(r:Double, g:Double, b:Double) async -&gt; [Color]{<br>        var firstColor, secondColor: Color<br>        var matchedColors = [Color]()<br>        do {<br>            urlComponents?.path = &quot;/scheme&quot;<br>            urlComponents?.queryItems = [<br>                URLQueryItem (name: &quot;rgb&quot;,<br>                              value:&quot;\(r),\(g),\(b)&quot;),<br>                URLQueryItem (name: &quot;mode&quot;, value: &quot;analogic-complement&quot;),<br>                URLQueryItem(name: &quot;count&quot;, value: &quot;3&quot;)<br>            ]<br>            <br>            let url = urlComponents?.url<br>            let request = URLRequest(url: url!)<br>            let (data,_) = try await URLSession.shared.data(for: request)<br><br>            self.colorsToFetch = try decoder.decode(ColorStruct.self, from: data)<br>            firstColor = hexToRGB(hex: colorsToFetch!.colors[0].hex.clean)<br>            secondColor = hexToRGB(hex: colorsToFetch!.colors[1].hex.clean)<br>            <br>            matchedColors.append(firstColor)<br>            matchedColors.append(secondColor)<br>            <br>        } catch {<br>            print (error.localizedDescription)<br>        }<br>        <br>        return matchedColors<br>    }<br>    <br>    private func hexToRGB(hex: String) -&gt; Color {<br>        let hex = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()<br>        if hex.count != 6 {<br>            return Color.white<br>        }<br>        var rgb: UInt64 = 0<br>        Scanner(string: hex).scanHexInt64(&amp;rgb)<br>        return Color(red: CGFloat((rgb &amp; 0xFF0000) &gt;&gt; 16) / 255.0,<br>                     green: CGFloat((rgb &amp; 0x00FF00) &gt;&gt; 8) / 255.0,<br>                     blue: CGFloat(rgb &amp; 0x0000FF) / 255.0)<br>    }<br><br>}</pre><p>That’s all for this post. I hope that I helped you with this little tutorial. Thank you for reading this post and enjoy with colors!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cde8b4874a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>