59264

how convert RGB or CMYK color to percentage CMYK - javascript

I use this snippet to convert RGB color to CMYK in javascript:

function RgbToCmyk(R,G,B) { if ((R == 0) && (G == 0) && (B == 0)) { return [0, 0, 0, 1]; } else { var calcR = 1 - (R / 255), calcG = 1 - (G / 255), calcB = 1 - (B / 255); var K = Math.min(calcR, Math.min(calcG, calcB)), C = (calcR - K) / (1 - K), M = (calcG - K) / (1 - K), Y = (calcB - K) / (1 - K); return [C, M, Y, K]; } }

now I want to convert returned CMYK to percentage CMYK.

for example this RGB color (171,215,170) become converted to this percentage CMYK (34%, 1%, 42%, 0)

(I used photoshop for converting)

<strong>EDIT:</strong> returned values of this snippet is between 0-1 . I found that I must change this snippet to returns values between 0-255 and then divided values by 2.55 to give me values of cmyk color as percentage. now how change this code to return values in range of 0-255 ??

Answer1:

CMYK and RGB are two different colour models (subtractive and additive models respectively), hence you need to have an algorithm to convert the values (to get closest equivalent of each system in the other one)

I'll suggest you to have a look here:

RGB-to-CMYK Color Conversion

using an algorithm like that you should be able to convert the values back and forth and then to get the percentage you just need to do a simple calculation, for example this function will return a percentage value of a given system:

function get_percentage(value, model){ if (model == "CMYK") return value; // CMYK values are 0-100 (if 0-1 just * 100) if (model == "RGB" ) return (value/ 255)* 100; } // a call with the value of 66 in RGB model: document.write( get_percentage( 66, "RGB"));

Hope it helps a bit,

Recommend

  • Algorithm - Attempting to balance out team skill levels, while having a equal amount of players
  • JVM (HotSpot) : Where do all the methods go ? method area ? native method stack?
  • Why do we need both Future and Promise?
  • How can I compute the frequencies of itemsets using R?
  • What's causing Java (hailstone sequence) to crash in my program
  • How to center this rotating image (CSS animation)
  • Changing the axis scale in numpy plot
  • Java - Multi-colored text
  • How to reset value of captured pixel
  • SSRS expression throws #Error - Possible reasons?
  • Match values in each group of a data.table column to values in a vector
  • How to do Calculation in Excel Cell upon leaving the cell
  • Centering a texture LibGDX
  • how to append two or more dataframes in pandas and do some analysis
  • html/css: how to create a hexagonal image-placeholder
  • AngularJS - ngBind and Bootstrap Switch
  • python struct.pack(): pack multiple datas in a list or a tuple
  • Put percent labels next to legend instead of in the slice
  • Is a .txt file created in VB different than one I'd randomly create?
  • C# fibonacci function returning errors
  • Default parameter as generic type
  • D3 get axis values on zoom event
  • Caching attributes in superclass
  • Custom validator control occupying space even though display set to dynamic
  • JSON response opens as a file, but I can't access it with JavaScript
  • Is it possible to access block's scope in method?
  • SignalR .NET Client Invoke throws an exception
  • Fill an image in a square container while keeping aspect ratio
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Return words with double consecutive letters
  • php design question - will a Helper help here?
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • apache spark aggregate function using min value
  • Reading document lines to the user (python)