<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: carlmelo</title>
    <description>The latest articles on DEV Community by carlmelo (@car1m310).</description>
    <link>https://dev.to/car1m310</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1046409%2F103129f6-29c2-4f6e-aee5-a2d5c4cab735.jpeg</url>
      <title>DEV Community: carlmelo</title>
      <link>https://dev.to/car1m310</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/car1m310"/>
    <language>en</language>
    <item>
      <title>I am trying to figure out if I can use JSON as a color-picker for a drawing app.</title>
      <dc:creator>carlmelo</dc:creator>
      <pubDate>Thu, 11 May 2023 10:20:59 +0000</pubDate>
      <link>https://dev.to/car1m310/i-am-trying-to-figure-out-how-to-draw-using-json-3mop</link>
      <guid>https://dev.to/car1m310/i-am-trying-to-figure-out-how-to-draw-using-json-3mop</guid>
      <description>&lt;p&gt;As the title says, I am trying to figure out if I can use JSON to pick colors from, in order, to fulfill the phase-1 final assignment for an HTTP API that includes, at least, 5 elements in an array with 3 attributes.&lt;/p&gt;

&lt;p&gt;By having primary and secondary colors, I would have 6 elements (red, yellow, blue, orange, green and violet) in an array. Its attributes would be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;color name &lt;/li&gt;
&lt;li&gt;color image&lt;/li&gt;
&lt;li&gt;color group (primary or secondary)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And, it would be displayed on the screen as a single page drawing app using HTML and Javascript. &lt;/p&gt;

&lt;p&gt;My app will be, called "!ward," which is "draw!" backwards. Clever, ha?&lt;/p&gt;

&lt;p&gt;Unfortunately, what isn't clever is that implementing it is not as straightforward as I had thought. I might not have the time, nor knowledge to implement the necessary work-around. But, according to my research, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One way to implement a color picker on HTML canvas is to use &lt;code&gt;getImageData&lt;/code&gt; to get the pixel color under the mouse cursor. This can be achieved by first drawing a gradient on the canvas, and then using &lt;code&gt;getImageData&lt;/code&gt; to get the color at the mouse position. Here's an example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
gradient.addColorStop(0, 'red');
gradient.addColorStop(0.2, 'orange');
gradient.addColorStop(0.4, 'yellow');
gradient.addColorStop(0.6, 'green');
gradient.addColorStop(0.8, 'blue');
gradient.addColorStop(1, 'purple');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);

canvas.addEventListener('click', function(event) {
  const x = event.pageX - canvas.offsetLeft;
  const y = event.pageY - canvas.offsetTop;
  const imageData = ctx.getImageData(x, y, 1, 1);
  const color = `rgb(\${imageData.data[0]}, \${imageData.data[1]}, \${imageData.data[2]})`;
  console.log(color);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Another way to implement a color picker on HTML canvas is to draw a color palette image on the canvas, and then use &lt;code&gt;getImageData&lt;/code&gt; to get the pixel color under the mouse cursor. Here's an example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const image = new Image();
image.src = 'color-palette.png';
image.onload = function() {
  ctx.drawImage(image, 0, 0);
};

canvas.addEventListener('click', function(event) {
  const x = event.pageX - canvas.offsetLeft;
  const y = event.pageY - canvas.offsetTop;
  const imageData = ctx.getImageData(x, y, 1, 1);
  const color = `rgb(\${imageData.data[0]}, \${imageData.data[1]}, \${imageData.data[2]})`;
  console.log(color);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In both examples, the &lt;code&gt;rgb&lt;/code&gt; value of the pixel under the mouse cursor is obtained using &lt;code&gt;getImageData&lt;/code&gt;, and can be used to set the fill/stroke color of shapes drawn on the canvas.&lt;br&gt;
JSON can be used to store and retrieve colors in these examples. For example, if you want to store the color selected by the user in a JSON object, you could do something like this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let selectedColor = { r: 255, g: 0, b: 0 }; // initial color is red

canvas.addEventListener('click', function(event) {
  const x = event.pageX - canvas.offsetLeft;
  const y = event.pageY - canvas.offsetTop;
  const imageData = ctx.getImageData(x, y, 1, 1);
  selectedColor.r = imageData.data[0];
  selectedColor.g = imageData.data[1];
  selectedColor.b = imageData.data[2];
  console.log(selectedColor);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This would update the &lt;code&gt;selectedColor&lt;/code&gt; object with the &lt;code&gt;rgb&lt;/code&gt; values of the pixel under the mouse cursor every time the user clicks on the canvas.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My challenge will be to try to make the JSON color-picker work. But, in case, I can't do it in time, I have code using HTML and Javascript for a simple drawing app.&lt;/p&gt;

&lt;p&gt;In conclusion, I have found that my original idea was flawed. Even, if I can get a JSON color-picker to work, it would be clunky compared to what I already have working. Even, though there are many different ways to write code, I feel that the simplest, easiest, most elegant way is the best way. In this regard, a JSON color-picker would simply not be used outside of fulfilling some checkbox that needed to be checked. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
