-
Notifications
You must be signed in to change notification settings - Fork 689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Ability to Use Color Class #796
base: master
Are you sure you want to change the base?
Conversation
Add the ability to set fill_color, stroke_color, etc. with something other than a string. This class should have a method color_str that returns a hex string representing the color.
Closing for now due to 1.9.3 issues. |
Apparently, in ruby 1.9.3 the stabby lambda, at least when used as a when in a case statement, can't have a space before the parenthesis.
OK ... fixed issues with stabby lambda and spaces ... which are apparently different between 1.9.3 and 2.0.0. |
Something like this will work, but I don't want to rely on generic APIs like I think I'd prefer something like For handling our base case, which is string representation for RGB, and array for CMYK, we'd just check for something like I know this is a rough sketch, but feel free to implement it if it makes sense. If it doesn't, I will work on an alternative patch when I find time, and we can discuss further from there. |
/cc @packetmonkey since this kind of stuff usually captures your interest. |
I'll take a look. My thought for It looks like you're recommending we go ahead an implement a Color class here (I assume that's what the |
Subclassing Ruby core classes is both bad design and can lead to unexpected internal errors because of optimizations at the language level, so I don't want to encourage it. There's nothing about colors that make them natural to represent as a string or array, it's just an implementation detail. With that in mind, a color class is indeed what we'll want here. |
(accidentally submitted incomplete) If I where to swing at this problem, I would probably come up with something similar to below class Prawn::RGBColor
def initialize(value)
@value = value
end
def to_pdf
# return PDF serialization
end
end
class Prawn::CMYKColor
def initialize(value)
@value = value
end
def to_pdf
# return PDF serialization
end
end
module Prawn
def color(val)
case val
when Prawn::RGBColor
when Prawn::CMYKColor
return val
when String return Prawn::RGBColor.new(val)
when Array return Prawn::CMYKColor.new(val)
else
raise ArgumentError, "cannot handle color type #{val.type}"
end
end
end That way you can set up your own PDF Color externally however you like and just pass it in. We could also loosen it up to accept any object that responds to to_pdf so we can accept any pdf serializable object. This solution may take more refactoring than we would like and I think we could solve similar problems to the pull request #724 trying to add additional ways to set colors. |
@packetmonkey This solves a different problem, though. A custom color class would most likely be used for dynamic determination of colors, not color types! The change you suggested might be welcome at the PDF::Core level, though. |
Fair call on not encouraging bad behavior. On question though ... right now (and one thing I kept in my implementation) was that if you did a followed by a ...
you get back a string. Same with my example MyColor or an array. |
@slabounty: Returning a meaningful value wasn't ever documented anywhere, I don't think. So it's currently undefined behavior, and we can change it. |
@sandal - Simplifies things a bit. Seems like this is pretty doable. I'll do some work on it and update the pull request. Thanks for the direction. |
@sandal - OK so the renderer PDF::Core::Renderer in pdf-core initializes the fill and stroke colors with "000000" (it doesn't look like it actually uses these anywhere, just stores them). Did we want to try to push the new classes down to the pdf-core or handle these initializers a different way. Should we always store the fill/stroke colors in the graphics_state as strings or arrays? I'm not coming up with a clean way to handle the core. |
@slabounty For right now it'd probably be OK to convert into whatever PDF::Core wants. A patch to |
Add the new color classes and a factory for them. Modify existing code to use them.
Add color classes. Modify prawn to use them internally. Make all specs work.
@sandal - OK check this out. It's not perfect (quite possibly not even good), but I think it's the direction you'd wanted to head in. Right now, it's kind of at the same place as when I started (you can't pass in your own classes and have them work), but that'll be easy to address if we get the other pieces right. |
@slabounty This hasn't been forgotten, it's just blocking on me. I think your patch proves the concept, but I'll want to change the interfaces a little bit, and won't know exactly how I want it until I drop down to the code level. I will play around with getting this integrated some time after the next release. |
3c5df3e
to
8309626
Compare
Add the ability to set fill_color, stroke_color, etc. with something
other than a string. This class should have a method color_str that
returns a hex string representing the color.
Accept classes that are "closely related" to String and Array (via the checks for respond_to?(:to_str) and respond_to?(:to_ary).
Add tests for the above. I only saw integration type tests, so that is what I added. If I missed some tests that should be there, please let me know.
I should note that it seems like there should be a Color class in here somewhere and that the fill_color and stroke_color should create these rather than keeping the String/Array directly. However, given how little I know about the code base and design, it's entirely possible, that I'm completely mistaken.